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.

325 lines
7.0KB

  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. /* Project management routines. */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <sys/fcntl.h>
  27. #include <errno.h>
  28. #include "Loggable.H"
  29. #include "Project.H"
  30. #include <FL/filename.H>
  31. #include "const.h"
  32. #include "util/debug.h"
  33. #include "util/file.h"
  34. #include "Mixer.H"
  35. const int PROJECT_VERSION = 1;
  36. const char *Project::_errstr[] =
  37. {
  38. "Not a Non-Mixer project",
  39. "Locked by another process",
  40. "Access denied",
  41. "Incompatible project version"
  42. };
  43. char Project::_name[256];
  44. char Project::_created_on[40];
  45. char Project::_path[512];
  46. bool Project::_is_open = false;
  47. int Project::_lockfd = 0;
  48. /***********/
  49. /* Private */
  50. /***********/
  51. void
  52. Project::set_name ( const char *name )
  53. {
  54. strcpy( Project::_name, name );
  55. if ( Project::_name[ strlen( Project::_name ) - 1 ] == '/' )
  56. Project::_name[ strlen( Project::_name ) - 1 ] = '\0';
  57. char *s = rindex( Project::_name, '/' );
  58. s = s ? s + 1 : Project::_name;
  59. memmove( Project::_name, s, strlen( s ) + 1 );
  60. for ( s = Project::_name; *s; ++s )
  61. if ( *s == '_' || *s == '-' )
  62. *s = ' ';
  63. }
  64. bool
  65. Project::write_info ( void )
  66. {
  67. FILE *fp;
  68. if ( ! ( fp = fopen( "info", "w" ) ) )
  69. {
  70. WARNING( "could not open project info file for writing." );
  71. return false;
  72. }
  73. char s[40];
  74. if ( ! *_created_on )
  75. {
  76. time_t t = time( NULL );
  77. ctime_r( &t, s );
  78. s[ strlen( s ) - 1 ] = '\0';
  79. }
  80. else
  81. strcpy( s, _created_on );
  82. fprintf( fp, "created by\n\t%s\ncreated on\n\t%s\nversion\n\t%d\n",
  83. APP_TITLE " " VERSION,
  84. s,
  85. PROJECT_VERSION );
  86. fclose( fp );
  87. return true;
  88. }
  89. bool
  90. Project::read_info ( int *version, char **creation_date )
  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. *version = 0;
  99. *creation_date = 0;
  100. char *name, *value;
  101. while ( fscanf( fp, "%a[^\n]\n\t%a[^\n]\n", &name, &value ) == 2 )
  102. {
  103. MESSAGE( "Info: %s = %s", name, value );
  104. if ( ! strcmp( name, "version" ) )
  105. *version = atoi( value );
  106. else if ( ! strcmp( name, "created on" ) )
  107. *creation_date = strdup( value );
  108. free( name );
  109. free( value );
  110. }
  111. fclose( fp );
  112. return true;
  113. }
  114. /**********/
  115. /* Public */
  116. /**********/
  117. /** Save out any settings and unjournaled state... */
  118. bool
  119. Project::save ( void )
  120. {
  121. if ( ! open() )
  122. return true;
  123. // tle->save_timeline_settings();
  124. return mixer->save();
  125. // return Loggable::save_unjournaled_state();
  126. }
  127. /** Close the project (reclaiming all memory) */
  128. bool
  129. Project::close ( void )
  130. {
  131. if ( ! open() )
  132. return true;
  133. if ( ! save() )
  134. return false;
  135. /* Loggable::close(); */
  136. /* // write_info(); */
  137. _is_open = false;
  138. *Project::_name = '\0';
  139. *Project::_created_on = '\0';
  140. release_lock( &_lockfd, ".lock" );
  141. return true;
  142. }
  143. /** Ensure a project is valid before opening it... */
  144. bool
  145. Project::validate ( const char *name )
  146. {
  147. bool r = true;
  148. char pwd[512];
  149. fl_filename_absolute( pwd, sizeof( pwd ), "." );
  150. if ( chdir( name ) )
  151. {
  152. WARNING( "Cannot change to project dir \"%s\"", name );
  153. return false;
  154. }
  155. if ( ! exists( "info" ) ||
  156. ! exists( "snapshot" ))
  157. {
  158. WARNING( "Not a Non-Mixer project: \"%s\"", name );
  159. r = false;
  160. }
  161. chdir( pwd );
  162. return r;
  163. }
  164. /** Try to open project /name/. Returns 0 if sucsessful, an error code
  165. * otherwise */
  166. int
  167. Project::open ( const char *name )
  168. {
  169. if ( ! validate( name ) )
  170. return E_INVALID;
  171. close();
  172. chdir( name );
  173. if ( ! acquire_lock( &_lockfd, ".lock" ) )
  174. return E_LOCKED;
  175. int version;
  176. char *creation_date;
  177. if ( ! read_info( &version, &creation_date ) )
  178. return E_INVALID;
  179. if ( version != PROJECT_VERSION )
  180. return E_VERSION;
  181. if ( ! Loggable::replay( "snapshot" ) )
  182. return E_INVALID;
  183. if ( creation_date )
  184. {
  185. strcpy( _created_on, creation_date );
  186. free( creation_date );
  187. }
  188. else
  189. *_created_on = 0;
  190. set_name( name );
  191. *_path = '\0';
  192. fl_filename_absolute( _path, sizeof( _path ), "." );
  193. _is_open = true;
  194. // tle->load_timeline_settings();
  195. // timeline->zoom_fit();
  196. MESSAGE( "Loaded project \"%s\"", name );
  197. return 0;
  198. }
  199. /** Create a new project /name/ from existing template
  200. * /template_name/ */
  201. bool
  202. Project::create ( const char *name, const char *template_name )
  203. {
  204. if ( exists( name ) )
  205. {
  206. WARNING( "Project already exists" );
  207. return false;
  208. }
  209. close();
  210. if ( mkdir( name, 0777 ) )
  211. {
  212. WARNING( "Cannot create project directory" );
  213. return false;
  214. }
  215. if ( chdir( name ) )
  216. FATAL( "WTF? Cannot change to new project directory" );
  217. mkdir( "sources", 0777 );
  218. creat( "snapshot", 0666 );
  219. /* TODO: copy template */
  220. write_info();
  221. if ( open( name ) == 0 )
  222. {
  223. // /* add the bare essentials */
  224. // timeline->beats_per_minute( 0, 120 );
  225. // timeline->time( 0, 4, 4 );
  226. MESSAGE( "Created project \"%s\" from template \"%s\"", name, template_name );
  227. return true;
  228. }
  229. else
  230. {
  231. WARNING( "Failed to open newly created project" );
  232. return false;
  233. }
  234. }
  235. /** Replace the journal with a snapshot of the current state */
  236. void
  237. Project::compact ( void )
  238. {
  239. Loggable::compact();
  240. }