Merge branch 'master' into windows
[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/revision.h"
29 #include "ardour/session.h"
30 #include "ardour/session_metadata.h"
31
32 using namespace PBD;
33
34 namespace ARDOUR
35 {
36
37 static void
38 snprintf_bounded_null_filled (char* target, size_t target_size, char const * fmt, ...)
39 {
40         char buf[target_size+1];
41         va_list ap;
42
43         va_start (ap, fmt);
44         vsnprintf (buf, target_size+1, fmt, ap);
45         va_end (ap);
46
47         memset (target, 0, target_size);
48         memcpy (target, buf, target_size);
49
50 }
51
52 BroadcastInfo::BroadcastInfo ()
53 {
54
55 }
56
57 void
58 BroadcastInfo::set_from_session (Session const & session, int64_t time_ref)
59 {
60         set_description (session.name());
61         set_time_reference (time_ref);
62         set_origination_time ();
63         set_originator ();
64         set_originator_ref_from_session (session);
65 }
66
67 void
68 BroadcastInfo::set_originator (std::string const & str)
69 {
70         _has_info = true;
71
72         if (!str.empty()) {
73                 AudioGrapher::BroadcastInfo::set_originator (str);
74                 return;
75         }
76
77         snprintf_bounded_null_filled (info->originator, sizeof (info->originator), Glib::get_real_name().c_str());
78 }
79
80 void
81 BroadcastInfo::set_originator_ref_from_session (Session const & /*session*/)
82 {
83         _has_info = true;
84
85         /* random code is 9 digits */
86
87         int random_code = g_random_int() % 999999999;
88
89         /* Serial number is 12 chars */
90
91         std::ostringstream serial_number;
92         serial_number << "ARDOUR" << "r" <<  std::setfill('0') << std::right << std::setw(5) << revision;
93
94         snprintf_bounded_null_filled (info->originator_reference, sizeof (info->originator_reference), "%2s%3s%12s%02d%02d%02d%9d",
95                   SessionMetadata::Metadata()->country().c_str(),
96                   SessionMetadata::Metadata()->organization().c_str(),
97                   serial_number.str().c_str(),
98                   _time.tm_hour,
99                   _time.tm_min,
100                   _time.tm_sec,
101                   random_code);
102
103 }
104
105 } // namespace ARDOUR
106