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 using namespace std;
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/python2.6/site-packages:%2")
72  .arg(path)
73  .arg(QProcessEnvironment::systemEnvironment().value("PYTHONPATH"))
74  .toUtf8().constData(), 1);
75 #endif
76 
77  int retval = cmdline.ConfigureLogging();
78  if (retval != GENERIC_EXIT_OK)
79  return retval;
80 
82  // Don't listen to console input
83  close(0);
84 
85  CleanupGuard callCleanup(cleanup);
86 
87 #ifndef _WIN32
88  QList<int> signallist;
89  signallist << SIGINT << SIGTERM << SIGSEGV << SIGABRT << SIGBUS << SIGFPE
90  << SIGILL;
91 #if ! CONFIG_DARWIN
92  signallist << SIGRTMIN;
93 #endif
94  SignalHandler::Init(signallist);
96 #endif
97 
99  if (!gContext->Init(false))
100  {
101  LOG(VB_GENERAL, LOG_ERR, "Failed to init MythContext, exiting.");
103  }
104 
105  myth_nice(19);
106 
107  MythTranslation::load("mythfrontend");
108 
109  LookerUpper *lookup = new LookerUpper();
110 
111  LOG(VB_GENERAL, LOG_INFO,
112  "Testing grabbers and metadata sites for functionality...");
113  if (!lookup->AllOK())
114  {
115  delete lookup;
116  return GENERIC_EXIT_NOT_OK;
117  }
118  LOG(VB_GENERAL, LOG_INFO,
119  "All grabbers tested and working. Continuing...");
120 
121  if (cmdline.toBool("jobid"))
122  {
123  uint chanid = 0;
124  QDateTime starttime;
125  int jobType = JOB_METADATA;
126 
127  if (!JobQueue::GetJobInfoFromID(cmdline.toInt("jobid"), jobType,
128  chanid, starttime))
129  {
130  LOG(VB_GENERAL, LOG_INFO,
131  QString("No valid job found for jobid: %1")
132  .arg(cmdline.toInt("jobid")));
133  return GENERIC_EXIT_NOT_OK;
134  }
135 
136  lookup->HandleSingleRecording(chanid, starttime,
137  cmdline.toBool("refresh-rules"));
138  }
139  else if (cmdline.toBool("chanid") && cmdline.toBool("starttime"))
140  lookup->HandleSingleRecording(cmdline.toUInt("chanid"),
141  cmdline.toDateTime("starttime"),
142  cmdline.toBool("refresh-rules"));
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  delete lookup;
166 
167  LOG(VB_GENERAL, LOG_NOTICE, "MythMetadataLookup run complete.");
168 
169  return GENERIC_EXIT_OK;
170 }
QDateTime toDateTime(const QString &key) const
Returns stored QVariant as a QDateTime, falling to default if not provided.
void HandleAllRecordings(bool updaterules=false)
Definition: lookup.cpp:56
Startup context for MythTV.
Definition: mythcontext.h:42
#define GENERIC_EXIT_OK
Exited with no error.
Definition: exitcodes.h:10
void HandleAllArtwork(bool aggressive=false)
Definition: lookup.cpp:115
bool toBool(const QString &key) const
Returns stored QVariant as a boolean.
static void PrintVersion(void)
Print application version information.
void HandleSingleRecording(const uint chanid, const QDateTime &starttime, bool updaterules=false)
Definition: lookup.cpp:36
#define MYTH_APPNAME_MYTHMETADATALOOKUP
void PrintHelp(void) const
Print command line option help.
static void Init(QList< int > &signallist, QObject *parent=nullptr)
unsigned int uint
Definition: compat.h:140
void HandleAllRecordingRules(void)
Definition: lookup.cpp:90
MythContext * gContext
This global variable contains the MythContext instance for the application.
Definition: mythcontext.cpp:63
unsigned sleep(unsigned int x)
Definition: compat.h:159
static bool GetJobInfoFromID(int jobID, int &jobType, uint &chanid, QDateTime &recstartts)
Definition: jobqueue.cpp:671
#define close
Definition: compat.h:16
static void CopyRuleInetrefsToRecordings()
Definition: lookup.cpp:197
static void load(const QString &module_name)
Load a QTranslator for the user's preferred language.
static void Done(void)
static void SetHandler(int signum, SigHandlerFunc handler)
MythCommFlagCommandLineParser cmdline
bool myth_nice(int val)
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
virtual bool Parse(int argc, const char *const *argv)
Loop through argv and populate arguments with values.
bool AllOK()
Definition: lookup.h:16
static void cleanup(VideoFilter *filter)
#define SIGHUP
Definition: compat.h:213
bool StillWorking()
Definition: lookup.cpp:30
#define MYTH_BINARY_VERSION
Update this whenever the plug-in ABI changes.
Definition: mythversion.h:16
#define setenv(x, y, z)
Definition: compat.h:156
bool Init(const bool gui=true, const bool promptForBackend=false, const bool disableAutoDiscovery=false, const bool ignoreDB=false)
#define GENERIC_EXIT_NO_MYTHCONTEXT
No MythContext available.
Definition: exitcodes.h:13
int main(int argc, char *argv[])
#define GENERIC_EXIT_INVALID_CMDLINE
Command line parse error.
Definition: exitcodes.h:15
#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.
int toInt(const QString &key) const
Returns stored QVariant as an integer, falling to default if not provided.
int ConfigureLogging(const QString &mask="general", unsigned int progress=0)
Read in logging options and initialize the logging interface.