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  QString name = cmdline.GetPassthrough();
60  bool notification = cmdline.toBool("notification");
61  QString text /* = "message" */ ;
62  QString timeout = "0";
63  QString image = "";
64  QString origin = "MythUtils";
65  QString description = "";
66  QString extra = "";
67  QString progress_text = "";
68  QString progress = "-1";
69  QString fullscreen = "false";
70  QString error = "false";
71  QString visibility = "0";
72  QString type = "normal";
73 
74  QString message = notification ? kNotification : kMessage;
75 
76  if (cmdline.toBool("udpport"))
77  port = (unsigned short)cmdline.toUInt("udpport");
78  if (cmdline.toBool("bcastaddr"))
79  address.setAddress(cmdline.toString("bcastaddr"));
80  if (cmdline.toBool("message_text"))
81  text = cmdline.toString("message_text");
82  message.replace("%message_text%", text);
83  if (cmdline.toBool("timeout"))
84  timeout = cmdline.toString("timeout");
85  message.replace("%timeout%", timeout);
86  if (notification)
87  {
88  if (cmdline.toBool("image"))
89  image = cmdline.toString("image");
90  message.replace("%image%", image);
91  if (cmdline.toBool("origin"))
92  origin = cmdline.toString("origin");
93  message.replace("%origin%", origin);
94  if (cmdline.toBool("description"))
95  description = cmdline.toString("description");
96  message.replace("%description%", description);
97  if (cmdline.toBool("extra"))
98  extra = cmdline.toString("extra");
99  message.replace("%extra%", extra);
100  if (cmdline.toBool("progress_text"))
101  progress_text = cmdline.toString("progress_text");
102  message.replace("%progress_text%", progress_text);
103  if (cmdline.toBool("progress"))
104  progress = cmdline.toString("progress");
105  message.replace("%progress%", progress);
106  if (cmdline.toBool("fullscreen"))
107  fullscreen = cmdline.toString("fullscreen");
108  message.replace("%fullscreen%", fullscreen);
109  if (cmdline.toBool("error"))
110  error = cmdline.toString("error");
111  message.replace("%error%", error);
112  if (cmdline.toBool("visibility"))
113  visibility = cmdline.toString("visibility");
114  message.replace("%visibility%", visibility);
115  if (cmdline.toBool("type"))
116  type = cmdline.toString("type");
117  message.replace("%type%", type);
118  }
119 
120  // extra optional argument
121  // in effeect not use as the above code provides default for all possible
122  // cases
123  QMap<QString,QString>::const_iterator i;
124  QMap<QString,QString> extras = cmdline.GetExtra();
125  for (i = extras.begin(); i != extras.end(); ++i)
126  {
127  QString kv_name = i.key();
128  QString kv_value = i.value();
129 
130  kv_name.replace("--", "");
131  cerr << "name: " << kv_name.toLocal8Bit().constData()
132  << " -- value: " << kv_value.toLocal8Bit().constData() << endl;
133 
134  kv_name.append("%");
135  kv_name.prepend("%");
136  message.replace(kv_name, kv_value);
137  }
138 
139  cout << "output:\n" << message.toLocal8Bit().constData() << endl;
140 
141  QUdpSocket *sock = new QUdpSocket();
142  QByteArray utf8 = message.toUtf8();
143 
144  int result = GENERIC_EXIT_OK;
145  if (sock->writeDatagram(utf8, address, port) < 0)
146  {
147  cout << "Failed to send UDP/XML packet" << endl;
148  result = GENERIC_EXIT_NOT_OK;
149  }
150  else
151  {
152  cout << "Sent UDP/XML packet to IP "
153  << address.toString().toLocal8Bit().constData()
154  << " and port: " << port << endl;
155  }
156 
157  sock->deleteLater();
158 
159  return result;
160 }
161 
163 {
164  utilMap["message"] = &SendMessage;
165  utilMap["printmtemplate"] = &PrintMTemplate;
166  utilMap["notification"] = &SendMessage;
167  utilMap["printntemplate"] = &PrintNTemplate;
168 }
169 
170 /* vim: set expandtab tabstop=4 shiftwidth=4: */
#define GENERIC_EXIT_OK
Exited with no error.
Definition: exitcodes.h:10
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:41
static int PrintNTemplate(const MythUtilCommandLineParser &)
static int SendMessage(const MythUtilCommandLineParser &cmdline)
bool toBool(QString key) const
Returns stored QVariant as a boolean.
static int PrintMTemplate(const MythUtilCommandLineParser &)
QMap< QString, UtilFunc > UtilMap
Definition: mythutil.h:16
QString GetPassthrough(void) const
Return any text supplied on the command line after a bare '–'.
void registerMessageUtils(UtilMap &utilMap)
const QString kNotification
QString toString(QString key) const
Returns stored QVariant as a QString, falling to default if not provided.
MythCommFlagCommandLineParser cmdline
const char * name
Definition: ParseText.cpp:339
const QString kMessage
#define GENERIC_EXIT_NOT_OK
Exited with error.
Definition: exitcodes.h:11
uint toUInt(QString key) const
Returns stored QVariant as an unsigned integer, falling to default if not provided.