00001 #include "httprequest.h"
00002 #include <qfileinfo.h>
00003 #include <qregexp.h>
00004 #include <qurl.h>
00005 #include <qdir.h>
00006 #include <limits.h>
00007 #include <unistd.h>
00008 #include "util.h"
00009
00010 #include <cstdlib>
00011
00012 #include "upnpmedia.h"
00013
00014 #define LOC QString("UPnpMedia: ")
00015
00017
00019
00020 UPnpMedia::UPnpMedia(bool runthread, bool ismaster)
00021 {
00022
00023 if ((runthread) && (ismaster))
00024 {
00025 pthread_t upnpmediathread;
00026 pthread_create(&upnpmediathread, NULL, doUPnpMediaThread, this);
00027 }
00028
00029 }
00030
00031 void UPnpMedia::RunRebuildLoop(void)
00032 {
00033
00034
00035 sleep(10);
00036
00037 while (1)
00038 {
00039
00040 BuildMediaMap();
00041
00042 sleep(1800 + (random()%8));
00043 }
00044 }
00045
00046 void *UPnpMedia::doUPnpMediaThread(void *param)
00047 {
00048 UPnpMedia *upnpmedia = (UPnpMedia*)param;
00049 upnpmedia->RunRebuildLoop();
00050
00051 return NULL;
00052 }
00053
00054 QString UPnpMedia::GetTitleName(QString fPath, QString fName)
00055 {
00056 if (m_mapTitleNames[fPath])
00057 {
00058 return m_mapTitleNames[fPath];
00059 }
00060 else
00061 return fName;
00062 }
00063
00064 QString UPnpMedia::GetCoverArt(QString fPath)
00065 {
00066 if (m_mapCoverArt[fPath])
00067 {
00068 return m_mapCoverArt[fPath];
00069 }
00070 else
00071 return "";
00072 }
00073
00075
00077
00078
00079 void UPnpMedia::FillMetaMaps(void)
00080 {
00081 MSqlQuery query(MSqlQuery::InitCon());
00082
00083 QString sSQL = "SELECT filename, title, coverfile FROM videometadata";
00084
00085 query.prepare ( sSQL );
00086 query.exec();
00087
00088 if (query.isActive() && query.size() > 0)
00089 {
00090 while(query.next())
00091 {
00092 m_mapTitleNames[query.value(0).toString()] = query.value(1)
00093 .toString();
00094 m_mapCoverArt[query.value(0).toString()] = query.value(2)
00095 .toString();
00096 }
00097 }
00098
00099 }
00100
00101
00102 int UPnpMedia::buildFileList(QString directory, int rootID, int itemID, MSqlQuery &query)
00103 {
00104
00105 int parentid;
00106 QDir vidDir(directory);
00107 QString title;
00108
00109
00110
00111 if (rootID > 0)
00112 parentid = rootID;
00113 else
00114 parentid = itemID;
00115
00116 vidDir.setSorting( QDir:: DirsFirst | QDir::Name );
00117 const QFileInfoList *List = vidDir.entryInfoList();
00118
00119 if (!List) return itemID;
00120 for (QFileInfoListIterator it(*List); it; ++it)
00121 {
00122 QFileInfo Info(*it.current());
00123 QString fName = Info.fileName();
00124 QString fPath = Info.filePath();
00125
00126 if (fName == "." ||
00127 fName == "..")
00128 {
00129 continue;
00130 }
00131
00132 if (Info.isDir())
00133 {
00134 itemID++;
00135
00136 query.prepare("INSERT INTO upnpmedia "
00137 "(intid, class, itemtype, parentid, itemproperties, "
00138 "filepath, filename, title, coverart) "
00139 "VALUES (:ITEMID, :ITEMCLASS, 'FOLDER', :PARENTID, '', "
00140 ":FILEPATH, :FILENAME, :TITLE, :COVERART)");
00141
00142 query.bindValue(":ITEMCLASS", sMediaType);
00143 query.bindValue(":ITEMID", itemID);
00144 query.bindValue(":PARENTID", parentid);
00145 query.bindValue(":FILEPATH", fPath);
00146 query.bindValue(":FILENAME", fName);
00147
00148 query.bindValue(":TITLE", GetTitleName(fPath,fName));
00149 query.bindValue(":COVERART", GetCoverArt(fPath));
00150
00151 query.exec();
00152
00153 itemID = buildFileList(Info.filePath(), 0, itemID, query);
00154 continue;
00155
00156 }
00157 else
00158 {
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173 itemID++;
00174
00175
00176
00177
00178
00179 query.prepare("INSERT INTO upnpmedia "
00180 "(intid, class, itemtype, parentid, itemproperties, "
00181 "filepath, filename, title, coverart) "
00182 "VALUES (:ITEMID, :ITEMCLASS, 'FILE', :PARENTID, '', "
00183 ":FILEPATH, :FILENAME, :TITLE, :COVERART)");
00184
00185 query.bindValue(":ITEMCLASS", sMediaType);
00186 query.bindValue(":ITEMID", itemID);
00187 query.bindValue(":PARENTID", parentid);
00188 query.bindValue(":FILEPATH", fPath);
00189 query.bindValue(":FILENAME", fName);
00190
00191 query.bindValue(":TITLE", GetTitleName(fPath,fName));
00192 query.bindValue(":COVERART", GetCoverArt(fPath));
00193
00194 query.exec();
00195
00196 }
00197 }
00198
00199 return itemID;
00200 }
00201
00202 void UPnpMedia::BuildMediaMap(void)
00203 {
00204 MSqlQuery query(MSqlQuery::InitCon());
00205
00206 QString RootVidDir;
00207 int filecount;
00208 int nextID;
00209
00210
00211 sMediaType = "VIDEO";
00212
00213 if (sMediaType == "VIDEO")
00214 {
00215
00216 RootVidDir = gContext->GetSetting("VideoStartupDir");
00217
00218 if ((!RootVidDir.isNull()) && (RootVidDir != ""))
00219 {
00220
00221 FillMetaMaps();
00222
00223 query.prepare("DELETE FROM upnpmedia WHERE class = :ITEMCLASS");
00224 query.bindValue(":ITEMCLASS", sMediaType);
00225 query.exec();
00226
00227 query.exec("LOCK TABLES upnpmedia WRITE");
00228
00229 VERBOSE(VB_UPNP, LOC + QString("VideoStartupDir = %1")
00230 .arg(RootVidDir));
00231
00232 QStringList parts = QStringList::split( ":", RootVidDir );
00233
00234 nextID = STARTING_VIDEO_OBJECTID;
00235
00236 for ( QStringList::Iterator it = parts.begin(); it != parts.end();
00237 ++it )
00238 {
00239 filecount = nextID;
00240
00241 VERBOSE(VB_GENERAL, LOC + QString("BuildMediaMap %1 scan "
00242 "starting in :%1:")
00243 .arg(sMediaType)
00244 .arg(*it));
00245
00246 nextID = buildFileList(*it,STARTING_VIDEO_OBJECTID, nextID,
00247 query);
00248 filecount = (filecount - nextID) * -1;
00249
00250 VERBOSE(VB_GENERAL, LOC + QString("BuildMediaMap Done. Found "
00251 "%1 objects").arg(filecount));
00252
00253 }
00254
00255 query.exec("UNLOCK TABLES");
00256
00257 }
00258 else
00259 {
00260 VERBOSE(VB_GENERAL, LOC + "BuildMediaMap - no VideoStartupDir set, "
00261 " skipping scan.");
00262 }
00263
00264 }
00265 else
00266 {
00267 VERBOSE(VB_GENERAL, LOC + QString("BuildMediaMap UNKNOWN MediaType %1 "
00268 ", skipping scan.").arg(sMediaType));
00269 }
00270
00271 }
00272
00273