MythTV  master
mythtv/programs/mythmetadatalookup/main.cpp
Go to the documentation of this file.
1 // C headers
2 #include <unistd.h>
3 
4 // C++ headers
5 #include <iostream>
6 #include <memory>
7 
8 // Qt headers
9 #include <QCoreApplication>
10 #include <QEventLoop>
11 #ifdef Q_OS_MAC
12 #include <QProcessEnvironment>
13 #endif
14 
15 // libmyth headers
16 #include "exitcodes.h"
17 #include "mythcontext.h"
18 #include "mythdb.h"
19 #include "mythversion.h"
20 #include "mythmiscutil.h"
21 #include "jobqueue.h"
22 #include "mythtranslation.h"
23 #include "mythconfig.h"
24 #include "commandlineparser.h"
25 #include "loggingserver.h"
26 #include "mythlogging.h"
27 #include "signalhandling.h"
28 #include "cleanupguard.h"
29 
30 #include "lookup.h"
31 
32 namespace
33 {
34  void cleanup()
35  {
36  delete gContext;
37  gContext = nullptr;
39  }
40 }
41 
42 bool inJobQueue = false;
43 
44 int main(int argc, char *argv[])
45 {
47  if (!cmdline.Parse(argc, argv))
48  {
51  }
52 
53  if (cmdline.toBool("showhelp"))
54  {
56  return GENERIC_EXIT_OK;
57  }
58 
59  if (cmdline.toBool("showversion"))
60  {
62  return GENERIC_EXIT_OK;
63  }
64 
65  QCoreApplication a(argc, argv);
66  QCoreApplication::setApplicationName(MYTH_APPNAME_MYTHMETADATALOOKUP);
67 
68 #ifdef Q_OS_MAC
69  QString path = QCoreApplication::applicationDirPath();
70  setenv("PYTHONPATH",
71  QString("%1/../Resources/lib/%2/site-packages:%3")
72  .arg(path)
73  .arg(QFileInfo(PYTHON_EXE).fileName())
74  .arg(QProcessEnvironment::systemEnvironment().value("PYTHONPATH"))
75  .toUtf8().constData(), 1);
76 #endif
77 
78  int retval = cmdline.ConfigureLogging();
79  if (retval != GENERIC_EXIT_OK)
80  return retval;
81 
83  // Don't listen to console input
84  close(0);
85 
86  CleanupGuard callCleanup(cleanup);
87 
88 #ifndef _WIN32
89  QList<int> signallist;
90  signallist << SIGINT << SIGTERM << SIGSEGV << SIGABRT << SIGBUS << SIGFPE
91  << SIGILL;
92 #if ! CONFIG_DARWIN
93  signallist << SIGRTMIN;
94 #endif
95  SignalHandler::Init(signallist);
97 #endif
98 
100  if (!gContext->Init(false))
101  {
102  LOG(VB_GENERAL, LOG_ERR, "Failed to init MythContext, exiting.");
104  }
105 
106  myth_nice(19);
107 
108  MythTranslation::load("mythfrontend");
109 
110  std::unique_ptr<LookerUpper> lookup {new LookerUpper};
111 
112  LOG(VB_GENERAL, LOG_INFO,
113  "Testing grabbers and metadata sites for functionality...");
114  if (!LookerUpper::AllOK())
115  return GENERIC_EXIT_NOT_OK;
116  LOG(VB_GENERAL, LOG_INFO,
117  "All grabbers tested and working. Continuing...");
118 
119  if (cmdline.toBool("jobid"))
120  {
121  uint chanid = 0;
122  QDateTime starttime;
123  int jobType = JOB_METADATA;
124 
125  if (!JobQueue::GetJobInfoFromID(cmdline.toInt("jobid"), jobType,
126  chanid, starttime))
127  {
128  LOG(VB_GENERAL, LOG_INFO,
129  QString("No valid job found for jobid: %1")
130  .arg(cmdline.toInt("jobid")));
131  return GENERIC_EXIT_NOT_OK;
132  }
133 
134  lookup->HandleSingleRecording(chanid, starttime,
135  cmdline.toBool("refresh-rules"));
136  }
137  else if (cmdline.toBool("chanid") && cmdline.toBool("starttime"))
138  {
139  lookup->HandleSingleRecording(cmdline.toUInt("chanid"),
140  cmdline.toDateTime("starttime"),
141  cmdline.toBool("refresh-rules"));
142  }
143  else if (cmdline.toBool("refresh-all-rules"))
144  {
145  lookup->HandleAllRecordingRules();
147  }
148  else if (cmdline.toBool("refresh-all-artwork"))
149  lookup->HandleAllArtwork(false);
150  else if (cmdline.toBool("refresh-all-artwork-dangerously"))
151  lookup->HandleAllArtwork(true);
152  else
153  {
154  // refresh-all is default behavior if no other arguments given
156  lookup->HandleAllRecordings(cmdline.toBool("refresh-rules"));
157  }
158 
159  while (lookup->StillWorking())
160  {
161  sleep(1);
162  qApp->processEvents();
163  }
164 
165  LOG(VB_GENERAL, LOG_NOTICE, "MythMetadataLookup run complete.");
166 
167  return GENERIC_EXIT_OK;
168 }
GENERIC_EXIT_OK
#define GENERIC_EXIT_OK
Exited with no error.
Definition: exitcodes.h:10
inJobQueue
bool inJobQueue
Definition: mythtv/programs/mythmetadatalookup/main.cpp:42
mythdb.h
MythContext
Startup context for MythTV.
Definition: mythcontext.h:43
setenv
#define setenv(x, y, z)
Definition: compat.h:157
arg
arg(title).arg(filename).arg(doDelete))
JobQueue::GetJobInfoFromID
static bool GetJobInfoFromID(int jobID, int &jobType, uint &chanid, QDateTime &recstartts)
Definition: jobqueue.cpp:673
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
GENERIC_EXIT_INVALID_CMDLINE
#define GENERIC_EXIT_INVALID_CMDLINE
Command line parse error.
Definition: exitcodes.h:15
LookerUpper
Definition: lookup.h:10
close
#define close
Definition: compat.h:17
mythversion.h
sleep
unsigned sleep(unsigned int x)
Definition: compat.h:160
MythCommandLineParser::Parse
virtual bool Parse(int argc, const char *const *argv)
Loop through argv and populate arguments with values.
Definition: mythcommandlineparser.cpp:1435
MythMetadataLookupCommandLineParser
Definition: mythmetadatalookup/commandlineparser.h:4
mythlogging.h
main
int main(int argc, char *argv[])
Definition: mythtv/programs/mythmetadatalookup/main.cpp:44
signalhandling.h
CleanupGuard
Definition: cleanupguard.h:6
SIGHUP
#define SIGHUP
Definition: compat.h:214
MythCommandLineParser::PrintVersion
static void PrintVersion(void)
Print application version information.
Definition: mythcommandlineparser.cpp:1263
mythtranslation.h
MYTH_APPNAME_MYTHMETADATALOOKUP
#define MYTH_APPNAME_MYTHMETADATALOOKUP
Definition: mythcorecontext.h:33
MythCommandLineParser::toUInt
uint toUInt(const QString &key) const
Returns stored QVariant as an unsigned integer, falling to default if not provided.
Definition: mythcommandlineparser.cpp:2116
MythCommandLineParser::PrintHelp
void PrintHelp(void) const
Print command line option help.
Definition: mythcommandlineparser.cpp:1279
jobqueue.h
uint
unsigned int uint
Definition: compat.h:140
LookerUpper::AllOK
static bool AllOK()
Definition: lookup.h:16
commandlineparser.h
cleanup
static QString cleanup(const QString &str)
Definition: remoteencoder.cpp:672
MYTH_BINARY_VERSION
#define MYTH_BINARY_VERSION
Update this whenever the plug-in ABI changes.
Definition: mythversion.h:15
cmdline
MythCommFlagCommandLineParser cmdline
Definition: mythtv/programs/mythcommflag/main.cpp:74
mythmiscutil.h
GENERIC_EXIT_NOT_OK
#define GENERIC_EXIT_NOT_OK
Exited with error.
Definition: exitcodes.h:11
MythCommandLineParser::toBool
bool toBool(const QString &key) const
Returns stored QVariant as a boolean.
Definition: mythcommandlineparser.cpp:2067
LookerUpper::CopyRuleInetrefsToRecordings
static void CopyRuleInetrefsToRecordings()
Definition: lookup.cpp:195
cleanupguard.h
JOB_METADATA
@ JOB_METADATA
Definition: jobqueue.h:75
mythcontext.h
GENERIC_EXIT_NO_MYTHCONTEXT
#define GENERIC_EXIT_NO_MYTHCONTEXT
No MythContext available.
Definition: exitcodes.h:13
SignalHandler::SetHandler
static void SetHandler(int signum, SigHandlerFunc handler)
Definition: signalhandling.cpp:138
lookup.h
MythCommandLineParser::ConfigureLogging
int ConfigureLogging(const QString &mask="general", bool progress=false)
Read in logging options and initialize the logging interface.
Definition: mythcommandlineparser.cpp:2718
myth_nice
bool myth_nice(int val)
Definition: mythmiscutil.cpp:704
MythTranslation::load
static void load(const QString &module_name)
Load a QTranslator for the user's preferred language.
Definition: mythtranslation.cpp:37
exitcodes.h
MythCommandLineParser::toInt
int toInt(const QString &key) const
Returns stored QVariant as an integer, falling to default if not provided.
Definition: mythcommandlineparser.cpp:2089
loggingserver.h
MythCommandLineParser::toDateTime
QDateTime toDateTime(const QString &key) const
Returns stored QVariant as a QDateTime, falling to default if not provided.
Definition: mythcommandlineparser.cpp:2321
gContext
MythContext * gContext
This global variable contains the MythContext instance for the application.
Definition: mythcontext.cpp:64
MythContext::Init
bool Init(bool gui=true, bool promptForBackend=false, bool disableAutoDiscovery=false, bool ignoreDB=false)
Definition: mythcontext.cpp:1569
SignalHandler::Done
static void Done(void)
Definition: signalhandling.cpp:131
SignalHandler::Init
static void Init(QList< int > &signallist, QObject *parent=nullptr)
Definition: signalhandling.cpp:124