more fixes/tweaks from the land of the lion
[ardour.git] / libs / ardour / broadcast_info.cc
1 /*
2     Copyright (C) 2008 Paul Davis
3     Author: Sakari Bergen
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #include "ardour/broadcast_info.h"
22 #include <iostream>
23 #include <sstream>
24 #include <iomanip>
25
26 #include <glibmm.h>
27
28 #include "ardour/svn_revision.h"
29 #include "ardour/ardour.h"
30 #include "ardour/session.h"
31 #include "ardour/session_metadata.h"
32
33 #include "pbd/convert.h"
34
35 using namespace PBD;
36
37 namespace ARDOUR
38 {
39
40 static void
41 snprintf_bounded_null_filled (char* target, size_t target_size, char const * fmt, ...)
42 {
43         char buf[target_size+1];
44         va_list ap;
45
46         va_start (ap, fmt);
47         vsnprintf (buf, target_size+1, fmt, ap);
48         va_end (ap);
49
50         memset (target, 0, target_size);
51         memcpy (target, buf, target_size);
52
53 }
54
55 BroadcastInfo::BroadcastInfo ()
56 {
57
58 }
59
60 void
61 BroadcastInfo::set_from_session (Session const & session, int64_t time_ref)
62 {
63         set_description (session.name());
64         set_time_reference (time_ref);
65         set_origination_time ();
66         set_originator ();
67         set_originator_ref_from_session (session);
68 }
69
70 void
71 BroadcastInfo::set_originator (std::string const & str)
72 {
73         _has_info = true;
74
75         if (!str.empty()) {
76                 AudioGrapher::BroadcastInfo::set_originator (str);
77                 return;
78         }
79
80         snprintf_bounded_null_filled (info->originator, sizeof (info->originator), Glib::get_real_name().c_str());
81 }
82
83 void
84 BroadcastInfo::set_originator_ref_from_session (Session const & /*session*/)
85 {
86         _has_info = true;
87
88         /* random code is 9 digits */
89
90         int random_code = random() % 999999999;
91
92         /* Serial number is 12 chars */
93
94         std::ostringstream serial_number;
95         serial_number << "ARDOUR" << "r" <<  std::setfill('0') << std::right << std::setw(5) << svn_revision;
96
97         snprintf_bounded_null_filled (info->originator_reference, sizeof (info->originator_reference), "%2s%3s%12s%02d%02d%02d%9d",
98                   SessionMetadata::Metadata()->country().c_str(),
99                   SessionMetadata::Metadata()->organization().c_str(),
100                   serial_number.str().c_str(),
101                   _time.tm_hour,
102                   _time.tm_min,
103                   _time.tm_sec,
104                   random_code);
105
106 }
107
108 } // namespace ARDOUR
109