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.

343 lines
7.4KB

  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. void
  65. Project::name ( const char *name )
  66. {
  67. strcpy( Project::_name, name );
  68. }
  69. bool
  70. Project::write_info ( void )
  71. {
  72. FILE *fp;
  73. if ( ! ( fp = fopen( "info", "w" ) ) )
  74. {
  75. WARNING( "could not open project info file for writing." );
  76. return false;
  77. }
  78. char s[40];
  79. if ( ! *_created_on )
  80. {
  81. time_t t = time( NULL );
  82. ctime_r( &t, s );
  83. s[ strlen( s ) - 1 ] = '\0';
  84. }
  85. else
  86. strcpy( s, _created_on );
  87. fprintf( fp, "created by\n\t%s\ncreated on\n\t%s\nversion\n\t%d\n",
  88. APP_TITLE " " VERSION,
  89. s,
  90. PROJECT_VERSION );
  91. fclose( fp );
  92. return true;
  93. }
  94. bool
  95. Project::read_info ( int *version, char **creation_date, char **created_by )
  96. {
  97. FILE *fp;
  98. if ( ! ( fp = fopen( "info", "r" ) ) )
  99. {
  100. WARNING( "could not open project info file for reading." );
  101. return false;
  102. }
  103. *version = 0;
  104. *creation_date = 0;
  105. *created_by = 0;
  106. char *name, *value;
  107. while ( fscanf( fp, "%a[^\n]\n\t%a[^\n]\n", &name, &value ) == 2 )
  108. {
  109. MESSAGE( "Info: %s = %s", name, value );
  110. if ( ! strcmp( name, "version" ) )
  111. *version = atoi( value );
  112. else if ( ! strcmp( name, "created on" ) )
  113. *creation_date = strdup( value );
  114. else if ( ! strcmp( name, "created by" ) )
  115. *created_by = strdup( value );
  116. free( name );
  117. free( value );
  118. }
  119. fclose( fp );
  120. return true;
  121. }
  122. /**********/
  123. /* Public */
  124. /**********/
  125. /** Save out any settings and unjournaled state... */
  126. bool
  127. Project::save ( void )
  128. {
  129. if ( ! open() )
  130. return true;
  131. // tle->save_timeline_settings();
  132. int r = mixer->save();
  133. Loggable::clear_dirty();
  134. return r;
  135. // return Loggable::save_unjournaled_state();
  136. }
  137. /** Close the project (reclaiming all memory) */
  138. bool
  139. Project::close ( void )
  140. {
  141. if ( ! open() )
  142. return true;
  143. if ( ! save() )
  144. return false;
  145. Loggable::close();
  146. /* // write_info(); */
  147. _is_open = false;
  148. *Project::_name = '\0';
  149. *Project::_created_on = '\0';
  150. release_lock( &_lockfd, ".lock" );
  151. return true;
  152. }
  153. /** Ensure a project is valid before opening it... */
  154. bool
  155. Project::validate ( const char *name )
  156. {
  157. bool r = true;
  158. char pwd[512];
  159. fl_filename_absolute( pwd, sizeof( pwd ), "." );
  160. if ( chdir( name ) )
  161. {
  162. WARNING( "Cannot change to project dir \"%s\"", name );
  163. return false;
  164. }
  165. if ( ! exists( "info" ) ||
  166. ! exists( "snapshot" ))
  167. {
  168. WARNING( "Not a Non-Mixer project: \"%s\"", name );
  169. r = false;
  170. }
  171. chdir( pwd );
  172. return r;
  173. }
  174. /** Try to open project /name/. Returns 0 if sucsessful, an error code
  175. * otherwise */
  176. int
  177. Project::open ( const char *name )
  178. {
  179. if ( ! validate( name ) )
  180. return E_INVALID;
  181. close();
  182. chdir( name );
  183. if ( ! acquire_lock( &_lockfd, ".lock" ) )
  184. return E_LOCKED;
  185. int version;
  186. char *creation_date;
  187. char *created_by;
  188. if ( ! read_info( &version, &creation_date, &created_by ) )
  189. return E_INVALID;
  190. if ( strncmp( created_by, APP_TITLE, strlen( APP_TITLE ) ) )
  191. return E_INVALID;
  192. if ( version != PROJECT_VERSION )
  193. return E_VERSION;
  194. if ( ! Loggable::replay( "snapshot" ) )
  195. return E_INVALID;
  196. if ( creation_date )
  197. {
  198. strcpy( _created_on, creation_date );
  199. free( creation_date );
  200. }
  201. else
  202. *_created_on = 0;
  203. set_name( name );
  204. *_path = '\0';
  205. fl_filename_absolute( _path, sizeof( _path ), "." );
  206. _is_open = true;
  207. // tle->load_timeline_settings();
  208. // timeline->zoom_fit();
  209. Loggable::clear_dirty();
  210. MESSAGE( "Loaded project \"%s\"", name );
  211. return 0;
  212. }
  213. /** Create a new project /name/ from existing template
  214. * /template_name/ */
  215. bool
  216. Project::create ( const char *name, const char *template_name )
  217. {
  218. if ( exists( name ) )
  219. {
  220. WARNING( "Project already exists" );
  221. return false;
  222. }
  223. close();
  224. if ( mkdir( name, 0777 ) )
  225. {
  226. WARNING( "Cannot create project directory" );
  227. return false;
  228. }
  229. if ( chdir( name ) )
  230. FATAL( "WTF? Cannot change to new project directory" );
  231. // mkdir( "sources", 0777 );
  232. creat( "snapshot", 0666 );
  233. /* TODO: copy template */
  234. write_info();
  235. if ( open( name ) == 0 )
  236. {
  237. // /* add the bare essentials */
  238. // timeline->beats_per_minute( 0, 120 );
  239. // timeline->time( 0, 4, 4 );
  240. MESSAGE( "Created project \"%s\" from template \"%s\"", name, template_name );
  241. return true;
  242. }
  243. else
  244. {
  245. WARNING( "Failed to open newly created project" );
  246. return false;
  247. }
  248. }
  249. /** Replace the journal with a snapshot of the current state */
  250. void
  251. Project::compact ( void )
  252. {
  253. Loggable::compact();
  254. }