MythTV  master
mythutil.cpp
Go to the documentation of this file.
1 // Qt includes
2 #include <QtGlobal>
3 #ifndef _WIN32
4 #include <QCoreApplication>
5 #else
6 #include <QApplication>
7 #endif
8 #include <QDateTime>
9 #include <QString>
10 
11 // libmyth* includes
12 #include "libmyth/mythcontext.h"
13 #include "libmythbase/exitcodes.h"
14 #include "libmythbase/mythconfig.h"
18 
19 // Local includes
20 #include "backendutils.h"
21 #include "eitutils.h"
22 #include "fileutils.h"
23 #include "jobutils.h"
24 #include "markuputils.h"
25 #include "messageutils.h"
26 #include "mpegutils.h"
27 #include "musicmetautils.h"
28 #include "mythutil.h"
30 #include "recordingutils.h"
31 
33  ProgramInfo &pginfo)
34 {
35  if (cmdline.toBool("video"))
36  {
37  QString video = cmdline.toString("video");
38  pginfo = ProgramInfo(video);
39  return true;
40  }
41  if (!cmdline.toBool("chanid"))
42  {
43  LOG(VB_GENERAL, LOG_ERR, "No chanid specified");
44  return false;
45  }
46 
47  if (!cmdline.toBool("starttime"))
48  {
49  LOG(VB_GENERAL, LOG_ERR, "No start time specified");
50  return false;
51  }
52 
53  uint chanid = cmdline.toUInt("chanid");
54  QDateTime starttime = cmdline.toDateTime("starttime");
55  QString startstring = starttime.toString("yyyyMMddhhmmss");
56 
57  const ProgramInfo tmpInfo(chanid, starttime);
58 
59  if (!tmpInfo.GetChanID())
60  {
61  LOG(VB_GENERAL, LOG_ERR,
62  QString("No program data exists for channel %1 at %2")
63  .arg(chanid).arg(startstring));
64  return false;
65  }
66 
67  pginfo = tmpInfo;
68 
69  return true;
70 }
71 
72 int main(int argc, char *argv[])
73 {
75  if (!cmdline.Parse(argc, argv))
76  {
79  }
80 
81  // default to quiet operation for pidcounter and pidfilter
82  QString defaultVerbose = "general";
83  LogLevel_t defaultLevel = LOG_INFO;
84  if (cmdline.toBool("pidcounter") || cmdline.toBool("pidfilter"))
85  {
86  if (!cmdline.toBool("verbose"))
87  {
88  verboseString = defaultVerbose = "";
89  verboseMask = 0;
90  }
91  if (!cmdline.toBool("loglevel"))
92  logLevel = defaultLevel = LOG_ERR;
93  }
94 
95 
96  if (cmdline.toBool("showhelp"))
97  {
99  return GENERIC_EXIT_OK;
100  }
101 
102  if (cmdline.toBool("showversion"))
103  {
105  return GENERIC_EXIT_OK;
106  }
107 
108 #ifndef _WIN32
109  QCoreApplication a(argc, argv);
110 #else
111  // MINGW application needs a window to receive messages
112  // such as socket notifications :[
113  QApplication a(argc, argv);
114 #endif
115 
116  QCoreApplication::setApplicationName(MYTH_APPNAME_MYTHUTIL);
117 
118  int retval = cmdline.ConfigureLogging(defaultVerbose);
119  if (retval != GENERIC_EXIT_OK)
120  return retval;
121 
122  if (!cmdline.toBool("loglevel"))
123  logLevel = defaultLevel;
124 
125 #ifndef _WIN32
127 #endif
128 
130  if (!gContext->Init(false))
131  {
132  LOG(VB_GENERAL, LOG_ERR, "Failed to init MythContext, exiting.");
134  }
135 
137 
138  UtilMap utilMap;
139 
140  registerBackendUtils(utilMap);
141  registerEITUtils(utilMap);
142  registerFileUtils(utilMap);
143  registerMPEGUtils(utilMap);
144  registerJobUtils(utilMap);
145  registerMarkupUtils(utilMap);
146  registerMessageUtils(utilMap);
147  registerMusicUtils(utilMap);
148  registerRecordingUtils(utilMap);
149 
150  bool cmdFound = false;
151  int cmdResult = GENERIC_EXIT_OK;
152  UtilMap::iterator i;
153  for (i = utilMap.begin(); i != utilMap.end(); ++i)
154  {
155  if (cmdline.toBool(i.key()))
156  {
157  cmdResult = (i.value())(cmdline);
158  cmdFound = true;
159  break;
160  }
161  }
162 
163  if(!cmdFound)
164  {
165  cmdline.PrintHelp();
166  cmdResult = GENERIC_EXIT_INVALID_CMDLINE;
167  }
168 
169  delete gContext;
170 
172 
173  return cmdResult;
174 }
175 
176 /* vim: set expandtab tabstop=4 shiftwidth=4: */
registerBackendUtils
void registerBackendUtils(UtilMap &utilMap)
Definition: backendutils.cpp:101
registerMarkupUtils
void registerMarkupUtils(UtilMap &utilMap)
Definition: markuputils.cpp:374
cmdline
MythCommFlagCommandLineParser cmdline
Definition: mythcommflag.cpp:72
logLevel
LogLevel_t logLevel
Definition: logging.cpp:89
musicmetautils.h
MythContext
Startup context for MythTV.
Definition: mythcontext.h:43
mpegutils.h
markuputils.h
backendutils.h
registerMessageUtils
void registerMessageUtils(UtilMap &utilMap)
Definition: messageutils.cpp:161
messageutils.h
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
fileutils.h
jobutils.h
GENERIC_EXIT_OK
@ GENERIC_EXIT_OK
Exited with no error.
Definition: exitcodes.h:11
MythUtilCommandLineParser
Definition: mythutil_commandlineparser.h:8
GetProgramInfo
bool GetProgramInfo(const MythUtilCommandLineParser &cmdline, ProgramInfo &pginfo)
Definition: mythutil.cpp:32
mythversion.h
verboseString
QString verboseString
Definition: logging.cpp:102
MythCommandLineParser::Parse
virtual bool Parse(int argc, const char *const *argv)
Loop through argv and populate arguments with values.
Definition: mythcommandlineparser.cpp:1553
eitutils.h
mythlogging.h
GENERIC_EXIT_NO_MYTHCONTEXT
@ GENERIC_EXIT_NO_MYTHCONTEXT
No MythContext available.
Definition: exitcodes.h:14
signalhandling.h
verboseMask
uint64_t verboseMask
Definition: logging.cpp:101
mythutil_commandlineparser.h
registerFileUtils
void registerFileUtils(UtilMap &utilMap)
Definition: fileutils.cpp:146
MythCommandLineParser::PrintVersion
static void PrintVersion(void)
Print application version information.
Definition: mythcommandlineparser.cpp:1381
MythCommandLineParser::toUInt
uint toUInt(const QString &key) const
Returns stored QVariant as an unsigned integer, falling to default if not provided.
Definition: mythcommandlineparser.cpp:2250
mythutil.h
MythCommandLineParser::PrintHelp
void PrintHelp(void) const
Print command line option help.
Definition: mythcommandlineparser.cpp:1397
MYTH_BINARY_VERSION
static constexpr const char * MYTH_BINARY_VERSION
Update this whenever the plug-in ABI changes.
Definition: mythversion.h:15
uint
unsigned int uint
Definition: compat.h:79
MythCommandLineParser::ApplySettingsOverride
void ApplySettingsOverride(void)
Apply all overrides to the global context.
Definition: mythcommandlineparser.cpp:2931
main
int main(int argc, char *argv[])
Definition: mythutil.cpp:72
registerMPEGUtils
void registerMPEGUtils(UtilMap &utilMap)
Definition: mpegutils.cpp:844
SignalHandler::Init
static void Init(QObject *parent=nullptr)
Definition: signalhandling.cpp:127
ProgramInfo::GetChanID
uint GetChanID(void) const
This is the unique key used in the database to locate tuning information.
Definition: programinfo.h:372
ProgramInfo
Holds information on recordings and videos.
Definition: programinfo.h:67
UtilMap
QMap< QString, UtilFunc > UtilMap
Definition: mythutil.h:15
MythCommandLineParser::toString
QString toString(const QString &key) const
Returns stored QVariant as a QString, falling to default if not provided.
Definition: mythcommandlineparser.cpp:2358
MythCommandLineParser::toBool
bool toBool(const QString &key) const
Returns stored QVariant as a boolean.
Definition: mythcommandlineparser.cpp:2201
registerEITUtils
void registerEITUtils(UtilMap &utilMap)
Definition: eitutils.cpp:137
mythcontext.h
recordingutils.h
MYTH_APPNAME_MYTHUTIL
static constexpr const char * MYTH_APPNAME_MYTHUTIL
Definition: mythcorecontext.h:37
registerMusicUtils
void registerMusicUtils(UtilMap &utilMap)
Definition: musicmetautils.cpp:586
MythCommandLineParser::ConfigureLogging
int ConfigureLogging(const QString &mask="general", bool progress=false)
Read in logging options and initialize the logging interface.
Definition: mythcommandlineparser.cpp:2862
exitcodes.h
GENERIC_EXIT_INVALID_CMDLINE
@ GENERIC_EXIT_INVALID_CMDLINE
Command line parse error.
Definition: exitcodes.h:16
MythCommandLineParser::toDateTime
QDateTime toDateTime(const QString &key) const
Returns stored QVariant as a QDateTime, falling to default if not provided.
Definition: mythcommandlineparser.cpp:2455
gContext
MythContext * gContext
This global variable contains the MythContext instance for the application.
Definition: mythcontext.cpp:55
registerRecordingUtils
void registerRecordingUtils(UtilMap &utilMap)
Definition: recordingutils.cpp:205
MythContext::Init
bool Init(bool gui=true, bool promptForBackend=false, bool disableAutoDiscovery=false, bool ignoreDB=false)
Definition: mythcontext.cpp:1562
SignalHandler::Done
static void Done(void)
Definition: signalhandling.cpp:134
registerJobUtils
void registerJobUtils(UtilMap &utilMap)
Definition: jobutils.cpp:71