00001
00002 #include "programinfoupdater.h"
00003 #include "mthreadpool.h"
00004 #include "mythlogging.h"
00005 #include "mythcorecontext.h"
00006 #include "remoteutil.h"
00007 #include "compat.h"
00008
00009 uint qHash(const PIKey &k)
00010 {
00011 return qHash(k.chanid) ^ qHash(k.recstartts.toTime_t());
00012 }
00013
00014 void ProgramInfoUpdater::insert(
00015 uint chanid, const QDateTime &recstartts,
00016 PIAction action, uint64_t filesize)
00017 {
00018 QMutexLocker locker(&lock);
00019 if (kPIUpdate == action || kPIUpdateFileSize == action)
00020 {
00021 PIKey key = PIKey(chanid, recstartts);
00022 QHash<PIKey,PIKeyData>::iterator it = needsUpdate.find(key);
00023
00024
00025
00026 if (it == needsUpdate.end())
00027 needsUpdate.insert(key, PIKeyData(action, filesize));
00028 else if (((*it).action == action) || (kPIUpdate == action))
00029 (*it) = PIKeyData(action, filesize);
00030 }
00031 else
00032 {
00033 needsAddDelete.push_back(PIKeyAction(chanid, recstartts, action));
00034 }
00035
00036
00037
00038 if (!isRunning)
00039 {
00040 isRunning = true;
00041 MThreadPool::globalInstance()->start(this, "ProgramInfoUpdater");
00042 }
00043 else
00044 moreWork.wakeAll();
00045 }
00046
00047 void ProgramInfoUpdater::run(void)
00048 {
00049 bool workDone;
00050
00051 do {
00052 workDone = false;
00053
00054
00055
00056
00057 usleep(50 * 1000);
00058
00059 lock.lock();
00060
00061
00062 vector<PIKeyAction>::iterator ita = needsAddDelete.begin();
00063 for (; ita != needsAddDelete.end(); ++ita)
00064 {
00065 if (kPIAdd != (*ita).action && kPIDelete != (*ita).action)
00066 continue;
00067
00068 QString type = (kPIAdd == (*ita).action) ? "ADD" : "DELETE";
00069 QString msg = QString("RECORDING_LIST_CHANGE %1 %2 %3")
00070 .arg(type).arg((*ita).chanid)
00071 .arg((*ita).recstartts.toString(Qt::ISODate));
00072
00073 workDone = true;
00074 gCoreContext->SendMessage(msg);
00075 }
00076 needsAddDelete.clear();
00077
00078
00079
00080 QHash<PIKey,PIKeyData>::iterator itu = needsUpdate.begin();
00081 for (; itu != needsUpdate.end(); ++itu)
00082 {
00083 QString msg;
00084
00085 if (kPIUpdateFileSize == (*itu).action)
00086 {
00087 msg = QString("UPDATE_FILE_SIZE %1 %2 %3")
00088 .arg(itu.key().chanid)
00089 .arg(itu.key().recstartts.toString(Qt::ISODate))
00090 .arg((*itu).filesize);
00091 }
00092 else
00093 {
00094 msg = QString("MASTER_UPDATE_PROG_INFO %1 %2")
00095 .arg(itu.key().chanid)
00096 .arg(itu.key().recstartts.toString(Qt::ISODate));
00097 }
00098
00099 workDone = true;
00100 gCoreContext->SendMessage(msg);
00101 }
00102 needsUpdate.clear();
00103
00104 if ( workDone )
00105 moreWork.wait(&lock, 1000);
00106
00107 lock.unlock();
00108 } while( workDone );
00109
00110 isRunning = false;
00111 }
00112
00113
00114
00115