MythTV  master
messageutils.cpp
Go to the documentation of this file.
1 // C++ includes
2 #include <iostream> // for cerr, cout
3 using std::cerr;
4 using std::cout;
5 using std::endl;
6 
7 // Qt headers
8 #include <QHostAddress>
9 #include <QUdpSocket>
10 #include <QString>
11 #include <QFile>
12 
13 // libmyth* includes
14 #include "compat.h"
15 #include "exitcodes.h"
16 #include "mythlogging.h"
17 
18 // Local includes
19 #include "messageutils.h"
20 
21 const QString kMessage =
22 "<mythmessage version=\"1\">\n"
23 " <text>%message_text%</text>\n"
24 " <timeout>%timeout%</timeout>\n"
25 "</mythmessage>";
26 
27 const QString kNotification =
28 "<mythnotification version=\"1\">\n"
29 " <text>%message_text%</text>\n"
30 " <origin>%origin%</origin>\n"
31 " <description>%description%</description>\n"
32 " <error>%error%</error>\n"
33 " <timeout>%timeout%</timeout>\n"
34 " <image>%image%</image>\n"
35 " <extra>%extra%</extra>\n"
36 " <progress_text>%progress_text%</progress_text>\n"
37 " <progress>%progress%</progress>\n"
38 " <fullscreen>%fullscreen%</fullscreen>\n"
39 " <visibility>%visibility%</visibility>\n"
40 " <type>%type%</type>\n"
41 "</mythnotification>";
42 
43 static int PrintMTemplate(const MythUtilCommandLineParser &/*cmdline*/)
44 {
45  cerr << kMessage.toLocal8Bit().constData() << endl;
46  return GENERIC_EXIT_OK;
47 }
48 
49 static int PrintNTemplate(const MythUtilCommandLineParser &/*cmdline*/)
50 {
51  cerr << kNotification.toLocal8Bit().constData() << endl;
52  return GENERIC_EXIT_OK;
53 }
54 
56 {
57  QHostAddress address = QHostAddress::Broadcast;
58  unsigned short port = 6948;
59  bool notification = cmdline.toBool("notification");
60  QString text /* = "message" */ ;
61  QString timeout = "0";
62  QString image = "";
63  QString origin = "MythUtils";
64  QString description = "";
65  QString extra = "";
66  QString progress_text = "";
67  QString progress = "-1";
68  QString fullscreen = "false";
69  QString error = "false";
70  QString visibility = "0";
71  QString type = "normal";
72 
73  QString message = notification ? kNotification : kMessage;
74 
75  if (cmdline.toBool("udpport"))
76  port = (unsigned short)cmdline.toUInt("udpport");
77  if (cmdline.toBool("bcastaddr"))
78  address.setAddress(cmdline.toString("bcastaddr"));
79  if (cmdline.toBool("message_text"))
80  text = cmdline.toString("message_text");
81  message.replace("%message_text%", text);
82  if (cmdline.toBool("timeout"))
83  timeout = cmdline.toString("timeout");
84  message.replace("%timeout%", timeout);
85  if (notification)
86  {
87  if (cmdline.toBool("image"))
88  image = cmdline.toString("image");
89  message.replace("%image%", image);
90  if (cmdline.toBool("origin"))
91  origin = cmdline.toString("origin");
92  message.replace("%origin%", origin);
93  if (cmdline.toBool("description"))
94  description = cmdline.toString("description");
95  message.replace("%description%", description);
96  if (cmdline.toBool("extra"))
97  extra = cmdline.toString("extra");
98  message.replace("%extra%", extra);
99  if (cmdline.toBool("progress_text"))
100  progress_text = cmdline.toString("progress_text");
101  message.replace("%progress_text%", progress_text);
102  if (cmdline.toBool("progress"))
103  progress = cmdline.toString("progress");
104  message.replace("%progress%", progress);
105  if (cmdline.toBool("fullscreen"))
106  fullscreen = cmdline.toString("fullscreen");
107  message.replace("%fullscreen%", fullscreen);
108  if (cmdline.toBool("error"))
109  error = cmdline.toString("error");
110  message.replace("%error%", error);
111  if (cmdline.toBool("visibility"))
112  visibility = cmdline.toString("visibility");
113  message.replace("%visibility%", visibility);
114  if (cmdline.toBool("type"))
115  type = cmdline.toString("type");
116  message.replace("%type%", type);
117  }
118 
119  // extra optional argument
120  // in effeect not use as the above code provides default for all possible
121  // cases
122  QMap<QString,QString>::const_iterator i;
123  QMap<QString,QString> extras = cmdline.GetExtra();
124  for (i = extras.begin(); i != extras.end(); ++i)
125  {
126  QString kv_name = i.key();
127  const QString& kv_value = i.value();
128 
129  kv_name.replace("--", "");
130  cerr << "name: " << kv_name.toLocal8Bit().constData()
131  << " -- value: " << kv_value.toLocal8Bit().constData() << endl;
132 
133  kv_name.append("%");
134  kv_name.prepend("%");
135  message.replace(kv_name, kv_value);
136  }
137 
138  cout << "output:\n" << message.toLocal8Bit().constData() << endl;
139 
140  QUdpSocket *sock = new QUdpSocket();
141  QByteArray utf8 = message.toUtf8();
142 
143  int result = GENERIC_EXIT_OK;
144  if (sock->writeDatagram(utf8, address, port) < 0)
145  {
146  cout << "Failed to send UDP/XML packet" << endl;
147  result = GENERIC_EXIT_NOT_OK;
148  }
149  else
150  {
151  cout << "Sent UDP/XML packet to IP "
152  << address.toString().toLocal8Bit().constData()
153  << " and port: " << port << endl;
154  }
155 
156  sock->deleteLater();
157 
158  return result;
159 }
160 
162 {
163  utilMap["message"] = &SendMessage;
164  utilMap["printmtemplate"] = &PrintMTemplate;
165  utilMap["notification"] = &SendMessage;
166  utilMap["printntemplate"] = &PrintNTemplate;
167 }
168 
169 /* vim: set expandtab tabstop=4 shiftwidth=4: */
#define GENERIC_EXIT_OK
Exited with no error.
Definition: exitcodes.h:10
bool toBool(const QString &key) const
Returns stored QVariant as a boolean.
QMap< QString, QString > GetExtra(void) const
Return map of additional key/value pairs provided on the command line independent of any registered a...
static void error(const char *str,...)
Definition: vbi.c:42
static int PrintNTemplate(const MythUtilCommandLineParser &)
static int SendMessage(const MythUtilCommandLineParser &cmdline)
static int PrintMTemplate(const MythUtilCommandLineParser &)
QMap< QString, UtilFunc > UtilMap
Definition: mythutil.h:16
QString toString(const QString &key) const
Returns stored QVariant as a QString, falling to default if not provided.
void registerMessageUtils(UtilMap &utilMap)
const QString kNotification
MythCommFlagCommandLineParser cmdline
const QString kMessage
#define GENERIC_EXIT_NOT_OK
Exited with error.
Definition: exitcodes.h:11
uint toUInt(const QString &key) const
Returns stored QVariant as an unsigned integer, falling to default if not provided.