MythTV  0.26-pre
myth.cpp
Go to the documentation of this file.
00001 
00002 // Program Name: myth.cpp
00003 // Created     : Jan. 19, 2010
00004 //
00005 // Copyright (c) 2010 David Blain <dblain@mythtv.org>
00006 //                                          
00007 // This library is free software; you can redistribute it and/or 
00008 // modify it under the terms of the GNU Lesser General Public
00009 // License as published by the Free Software Foundation; either
00010 // version 2.1 of the License, or at your option any later version of the LGPL.
00011 //
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 // Lesser General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU Lesser General Public
00018 // License along with this library.  If not, see <http://www.gnu.org/licenses/>.
00019 //
00021 
00022 #include "myth.h"
00023 
00024 #include <QDir>
00025 #include <QCryptographicHash>
00026 #include <QHostAddress>
00027 #include <QUdpSocket>
00028 
00029 #include "version.h"
00030 #include "mythversion.h"
00031 #include "mythcorecontext.h"
00032 #include "mythdbcon.h"
00033 #include "mythlogging.h"
00034 #include "storagegroup.h"
00035 #include "dbutil.h"
00036 #include "hardwareprofile.h"
00037 #include "mythmiscutil.h"
00038 
00040 //
00042 
00043 DTC::ConnectionInfo* Myth::GetConnectionInfo( const QString  &sPin )
00044 {
00045     QString sSecurityPin = gCoreContext->GetSetting( "SecurityPin", "");
00046 
00047     if ( sSecurityPin.isEmpty() )
00048         throw( QString( "No Security Pin assigned. Run mythtv-setup to set one." ));
00049         //SB: UPnPResult_HumanInterventionRequired,
00050 
00051     if ((sSecurityPin != "0000" ) && ( sPin != sSecurityPin ))
00052         throw( QString( "Not Authorized" ));
00053         //SB: UPnPResult_ActionNotAuthorized );
00054   
00055     DatabaseParams params = gCoreContext->GetDatabaseParams();
00056 
00057     // ----------------------------------------------------------------------
00058     // Check for DBHostName of "localhost" and change to public name or IP
00059     // ----------------------------------------------------------------------
00060 
00061     QString sServerIP = gCoreContext->GetSetting( "BackendServerIP", "localhost" );
00062     //QString sPeerIP   = pRequest->GetPeerAddress();
00063 
00064     if ((params.dbHostName == "localhost") &&
00065         (sServerIP         != "localhost"))  // &&
00066         //(sServerIP         != sPeerIP    ))
00067     {
00068         params.dbHostName = sServerIP;
00069     }
00070 
00071     // ----------------------------------------------------------------------
00072     // Create and populate a ConnectionInfo object
00073     // ----------------------------------------------------------------------
00074 
00075     DTC::ConnectionInfo *pInfo     = new DTC::ConnectionInfo();
00076     DTC::DatabaseInfo   *pDatabase = pInfo->Database();
00077     DTC::WOLInfo        *pWOL      = pInfo->WOL();
00078     DTC::VersionInfo    *pVersion  = pInfo->Version();
00079 
00080     pDatabase->setHost         ( params.dbHostName    );
00081     pDatabase->setPing         ( params.dbHostPing    );
00082     pDatabase->setPort         ( params.dbPort        );
00083     pDatabase->setUserName     ( params.dbUserName    );
00084     pDatabase->setPassword     ( params.dbPassword    );
00085     pDatabase->setName         ( params.dbName        );
00086     pDatabase->setType         ( params.dbType        );
00087     pDatabase->setLocalEnabled ( params.localEnabled  );
00088     pDatabase->setLocalHostName( params.localHostName );
00089 
00090     pWOL->setEnabled           ( params.wolEnabled   );
00091     pWOL->setReconnect         ( params.wolReconnect );
00092     pWOL->setRetry             ( params.wolRetry     );
00093     pWOL->setCommand           ( params.wolCommand   );
00094 
00095     pVersion->setVersion       ( MYTH_SOURCE_VERSION   );
00096     pVersion->setBranch        ( MYTH_SOURCE_PATH      );
00097     pVersion->setProtocol      ( MYTH_PROTO_VERSION    );
00098     pVersion->setBinary        ( MYTH_BINARY_VERSION   );
00099     pVersion->setSchema        ( MYTH_DATABASE_VERSION );
00100 
00101     // ----------------------------------------------------------------------
00102     // Return the pointer... caller is responsible to delete it!!!
00103     // ----------------------------------------------------------------------
00104 
00105     return pInfo;
00106 }
00107 
00109 //
00111 
00112 QString Myth::GetHostName( )
00113 {
00114     if (!gCoreContext)
00115         throw( QString( "No MythCoreContext in GetHostName." ));
00116 
00117     return gCoreContext->GetHostName();
00118 }
00120 //
00122 
00123 QStringList Myth::GetHosts( )
00124 {
00125     MSqlQuery query(MSqlQuery::InitCon());
00126 
00127     if (!query.isConnected())
00128         throw( QString( "Database not open while trying to load list of hosts" ));
00129 
00130     query.prepare(
00131         "SELECT DISTINCTROW hostname "
00132         "FROM settings "
00133         "WHERE (not isNull( hostname ))");
00134 
00135     if (!query.exec())
00136     {
00137         MythDB::DBError("MythAPI::GetHosts()", query);
00138 
00139         throw( QString( "Database Error executing query." ));
00140     }
00141 
00142     // ----------------------------------------------------------------------
00143     // return the results of the query
00144     // ----------------------------------------------------------------------
00145 
00146     QStringList oList;
00147 
00148     while (query.next())
00149         oList.append( query.value(0).toString() );
00150 
00151     return oList;
00152 }
00153 
00155 //
00157 
00158 QStringList Myth::GetKeys() 
00159 {
00160     MSqlQuery query(MSqlQuery::InitCon());
00161 
00162     if (!query.isConnected())
00163         throw( QString("Database not open while trying to load settings"));
00164 
00165     query.prepare("SELECT DISTINCTROW value FROM settings;" );
00166 
00167     if (!query.exec())
00168     {
00169         MythDB::DBError("MythAPI::GetKeys()", query);
00170 
00171         throw( QString( "Database Error executing query." ));
00172     }
00173 
00174     // ----------------------------------------------------------------------
00175     // return the results of the query
00176     // ----------------------------------------------------------------------
00177 
00178     QStringList oResults;
00179     
00180     //pResults->setObjectName( "KeyList" );
00181 
00182     while (query.next())
00183         oResults.append( query.value(0).toString() );
00184 
00185     return oResults;
00186 }
00187 
00189 //
00191 
00192 DTC::StorageGroupDirList *Myth::GetStorageGroupDirs( const QString &sGroupName,
00193                                                      const QString &sHostName ) 
00194 {
00195     MSqlQuery query(MSqlQuery::InitCon());
00196 
00197     if (!query.isConnected())
00198         throw( QString("Database not open while trying to list "
00199                        "Storage Group Dirs"));
00200 
00201     if (!sGroupName.isEmpty() && !sHostName.isEmpty())
00202     {
00203         query.prepare("SELECT id, groupname, hostname, dirname "
00204                       "FROM storagegroup "
00205                       "WHERE groupname = :GROUP AND hostname = :HOST "
00206                       "ORDER BY groupname, hostname, dirname" );
00207         query.bindValue(":HOST",  sHostName);
00208         query.bindValue(":GROUP", sGroupName);
00209     }
00210     else if (!sHostName.isEmpty())
00211     {
00212         query.prepare("SELECT id, groupname, hostname, dirname "
00213                       "FROM storagegroup "
00214                       "WHERE hostname = :HOST "
00215                       "ORDER BY groupname, hostname, dirname" );
00216         query.bindValue(":HOST",  sHostName);
00217     }
00218     else if (!sGroupName.isEmpty())
00219     {
00220         query.prepare("SELECT id, groupname, hostname, dirname "
00221                       "FROM storagegroup "
00222                       "WHERE groupname = :GROUP "
00223                       "ORDER BY groupname, hostname, dirname" );
00224         query.bindValue(":GROUP", sGroupName);
00225     }
00226     else
00227         query.prepare("SELECT id, groupname, hostname, dirname "
00228                       "FROM storagegroup "
00229                       "ORDER BY groupname, hostname, dirname" );
00230 
00231     if (!query.exec())
00232     {
00233         MythDB::DBError("MythAPI::GetStorageGroupDirs()", query);
00234 
00235         throw( QString( "Database Error executing query." ));
00236     }
00237 
00238     // ----------------------------------------------------------------------
00239     // return the results of the query
00240     // ----------------------------------------------------------------------
00241 
00242     DTC::StorageGroupDirList* pList = new DTC::StorageGroupDirList();
00243 
00244     while (query.next())
00245     {
00246         DTC::StorageGroupDir *pStorageGroupDir = pList->AddNewStorageGroupDir();
00247             
00248         pStorageGroupDir->setId            ( query.value(0).toInt()       );
00249         pStorageGroupDir->setGroupName     ( query.value(1).toString()    );
00250         pStorageGroupDir->setHostName      ( query.value(2).toString()    );
00251         pStorageGroupDir->setDirName       ( query.value(3).toString()    );
00252     }
00253 
00254     return pList;
00255 }
00256 
00258 //
00260 
00261 bool Myth::AddStorageGroupDir( const QString &sGroupName,
00262                                const QString &sDirName, 
00263                                const QString &sHostName )
00264 {
00265     MSqlQuery query(MSqlQuery::InitCon());
00266 
00267     if (!query.isConnected())
00268         throw( QString("Database not open while trying to add Storage Group "
00269                        "dir"));
00270 
00271     if (sGroupName.isEmpty())
00272         throw ( QString( "Storage Group Required" ));
00273 
00274     if (sDirName.isEmpty())
00275         throw ( QString( "Directory Name Required" ));
00276 
00277     if (sHostName.isEmpty())
00278         throw ( QString( "HostName Required" ));
00279 
00280     query.prepare("SELECT COUNT(*) "
00281                   "FROM storagegroup "
00282                   "WHERE groupname = :GROUPNAME "
00283                   "AND dirname = :DIRNAME "
00284                   "AND hostname = :HOSTNAME;");
00285     query.bindValue(":GROUPNAME", sGroupName );
00286     query.bindValue(":DIRNAME"  , sDirName   );
00287     query.bindValue(":HOSTNAME" , sHostName  );
00288     if (!query.exec())
00289     {
00290         MythDB::DBError("MythAPI::AddStorageGroupDir()", query);
00291 
00292         throw( QString( "Database Error executing query." ));
00293     }
00294 
00295     if (query.next())
00296     {
00297         if (query.value(0).toInt() > 0)
00298             return false;
00299     }
00300 
00301     query.prepare("INSERT storagegroup "
00302                   "( groupname, dirname, hostname ) "
00303                   "VALUES "
00304                   "( :GROUPNAME, :DIRNAME, :HOSTNAME );");
00305     query.bindValue(":GROUPNAME", sGroupName );
00306     query.bindValue(":DIRNAME"  , sDirName   );
00307     query.bindValue(":HOSTNAME" , sHostName  );
00308 
00309     if (!query.exec())
00310     {
00311         MythDB::DBError("MythAPI::AddStorageGroupDir()", query);
00312 
00313         throw( QString( "Database Error executing query." ));
00314     }
00315 
00316     return true;
00317 }
00318 
00320 //
00322 
00323 bool Myth::RemoveStorageGroupDir( const QString &sGroupName,
00324                                   const QString &sDirName, 
00325                                   const QString &sHostName )
00326 {
00327     MSqlQuery query(MSqlQuery::InitCon());
00328 
00329     if (!query.isConnected())
00330         throw( QString("Database not open while trying to remove Storage "
00331                        "Group dir"));
00332 
00333     if (sGroupName.isEmpty())
00334         throw ( QString( "Storage Group Required" ));
00335 
00336     if (sDirName.isEmpty())
00337         throw ( QString( "Directory Name Required" ));
00338 
00339     if (sHostName.isEmpty())
00340         throw ( QString( "HostName Required" ));
00341 
00342     query.prepare("DELETE "
00343                   "FROM storagegroup "
00344                   "WHERE groupname = :GROUPNAME "
00345                   "AND dirname = :DIRNAME "
00346                   "AND hostname = :HOSTNAME;");
00347     query.bindValue(":GROUPNAME", sGroupName );
00348     query.bindValue(":DIRNAME"  , sDirName   );
00349     query.bindValue(":HOSTNAME" , sHostName  );
00350     if (!query.exec())
00351     {
00352         MythDB::DBError("MythAPI::AddStorageGroupDir()", query);
00353 
00354         throw( QString( "Database Error executing query." ));
00355     }
00356 
00357     return true;
00358 }
00359 
00361 //
00363 
00364 DTC::TimeZoneInfo *Myth::GetTimeZone(  )
00365 {
00366     DTC::TimeZoneInfo *pResults = new DTC::TimeZoneInfo();
00367 
00368     pResults->setTimeZoneID( getTimeZoneID() );
00369     pResults->setUTCOffset( calc_utc_offset() );
00370     pResults->setCurrentDateTime( mythCurrentDateTime() );
00371 
00372     return pResults;
00373 }
00374 
00376 //
00378 
00379 DTC::LogMessageList *Myth::GetLogs(  const QString   &HostName,
00380                                      const QString   &Application,
00381                                      int             PID,
00382                                      int             TID,
00383                                      const QString   &Thread,
00384                                      const QString   &Filename,
00385                                      int             Line,
00386                                      const QString   &Function,
00387                                      const QDateTime &FromTime,
00388                                      const QDateTime &ToTime,
00389                                      const QString   &Level,
00390                                      const QString   &MsgContains )
00391 {
00392     DTC::LogMessageList *pList = new DTC::LogMessageList();
00393 
00394     MSqlQuery query(MSqlQuery::InitCon());
00395 
00396     // Get host name list
00397     QString sql = "SELECT DISTINCT host FROM logging ORDER BY host ASC";
00398     if (!query.exec(sql))
00399     {
00400         MythDB::DBError("Retrieving log host names", query);
00401         throw( QString( "Database Error executing query." ));
00402     }
00403     while (query.next())
00404     {
00405         DTC::LabelValue *pLabelValue = pList->AddNewHostName();
00406         QString availableHostName = query.value(0).toString();
00407         pLabelValue->setValue   ( availableHostName );
00408         pLabelValue->setActive  ( availableHostName == HostName );
00409         pLabelValue->setSelected( availableHostName == HostName );
00410     }
00411     // Get application list
00412     sql = "SELECT DISTINCT application FROM logging ORDER BY application ASC";
00413     if (!query.exec(sql))
00414     {
00415         MythDB::DBError("Retrieving log applications", query);
00416         throw( QString( "Database Error executing query." ));
00417     }
00418     while (query.next())
00419     {
00420         DTC::LabelValue *pLabelValue = pList->AddNewApplication();
00421         QString availableApplication = query.value(0).toString();
00422         pLabelValue->setValue   ( availableApplication );
00423         pLabelValue->setActive  ( availableApplication == Application );
00424         pLabelValue->setSelected( availableApplication == Application );
00425     }
00426 
00427     if (!HostName.isEmpty() && !Application.isEmpty())
00428     {
00429         // Get log messages
00430         sql = "SELECT host, application, pid, tid, thread, filename, "
00431               "       line, function, msgtime, level, message "
00432               "  FROM logging "
00433               " WHERE host = COALESCE(:HOSTNAME, host) "
00434               "   AND application = COALESCE(:APPLICATION, application) "
00435               "   AND pid = COALESCE(:PID, pid) "
00436               "   AND tid = COALESCE(:TID, tid) "
00437               "   AND thread = COALESCE(:THREAD, thread) "
00438               "   AND filename = COALESCE(:FILENAME, filename) "
00439               "   AND line = COALESCE(:LINE, line) "
00440               "   AND function = COALESCE(:FUNCTION, function) "
00441               "   AND msgtime >= COALESCE(:FROMTIME, msgtime) "
00442               "   AND msgtime <= COALESCE(:TOTIME, msgtime) "
00443               "   AND level <= COALESCE(:LEVEL, level) "
00444               ;
00445         if (!MsgContains.isEmpty())
00446         {
00447             sql.append("   AND message LIKE :MSGCONTAINS ");
00448         }
00449         sql.append(" ORDER BY msgtime ASC;");
00450 
00451         query.prepare(sql);
00452 
00453         query.bindValue(":HOSTNAME", (HostName.isEmpty()) ? QString() : HostName);
00454         query.bindValue(":APPLICATION", (Application.isEmpty()) ? QString() :
00455                                                                   Application);
00456         query.bindValue(":PID", ( PID == 0 ) ? QVariant(QVariant::ULongLong) :
00457                                                (qint64)PID);
00458         query.bindValue(":TID", ( TID == 0 ) ? QVariant(QVariant::ULongLong) :
00459                                                (qint64)TID);
00460         query.bindValue(":THREAD", (Thread.isEmpty()) ? QString() : Thread);
00461         query.bindValue(":FILENAME", (Filename.isEmpty()) ? QString() : Filename);
00462         query.bindValue(":LINE", ( Line == 0 ) ? QVariant(QVariant::ULongLong) :
00463                                                  (qint64)Line);
00464         query.bindValue(":FUNCTION", (Function.isEmpty()) ? QString() : Function);
00465         query.bindValue(":FROMTIME", (FromTime.isValid()) ? FromTime : QDateTime());
00466         query.bindValue(":TOTIME", (ToTime.isValid()) ? ToTime : QDateTime());
00467         query.bindValue(":LEVEL", (Level.isEmpty()) ?
00468                                         QVariant(QVariant::ULongLong) :
00469                                         (qint64)logLevelGet(Level));
00470 
00471         if (!MsgContains.isEmpty())
00472         {
00473             query.bindValue(":MSGCONTAINS", "%" + MsgContains + "%" );
00474         }
00475 
00476         if (!query.exec())
00477         {
00478             MythDB::DBError("Retrieving log messages", query);
00479             throw( QString( "Database Error executing query." ));
00480         }
00481 
00482         while (query.next())
00483         {
00484             DTC::LogMessage *pLogMessage = pList->AddNewLogMessage();
00485 
00486             pLogMessage->setHostName( query.value(0).toString() );
00487             pLogMessage->setApplication( query.value(1).toString() );
00488             pLogMessage->setPID( query.value(2).toInt() );
00489             pLogMessage->setTID( query.value(3).toInt() );
00490             pLogMessage->setThread( query.value(4).toString() );
00491             pLogMessage->setFilename( query.value(5).toString() );
00492             pLogMessage->setLine( query.value(6).toInt() );
00493             pLogMessage->setFunction( query.value(7).toString() );
00494             pLogMessage->setTime( query.value(8).toDateTime() );
00495             pLogMessage->setLevel( logLevelGetName(
00496                                        (LogLevel_t)query.value(9).toInt()) );
00497             pLogMessage->setMessage( query.value(10).toString() );
00498         }
00499     }
00500 
00501     return pList;
00502 }
00503 
00505 //
00507 
00508 DTC::SettingList *Myth::GetSetting( const QString &sHostName, 
00509                                     const QString &sKey, 
00510                                     const QString &sDefault )
00511 {
00512 
00513     MSqlQuery query(MSqlQuery::InitCon());
00514 
00515     if (!query.isConnected())
00516     {
00517         throw( QString("Database not open while trying to load setting: %1")
00518                   .arg( sKey ));
00519     }
00520 
00521     // ----------------------------------------------------------------------
00522 
00523     DTC::SettingList *pList = new DTC::SettingList();
00524 
00525     //pList->setObjectName( "Settings" );
00526     pList->setHostName  ( sHostName  );
00527 
00528     // ----------------------------------------------------------------------
00529     // Format Results
00530     // ----------------------------------------------------------------------
00531 
00532     if (!sKey.isEmpty())
00533     {
00534         // ------------------------------------------------------------------
00535         // Key Supplied, lookup just its value.
00536         // ------------------------------------------------------------------
00537 
00538         query.prepare("SELECT data, hostname from settings "
00539                       "WHERE value = :KEY AND "
00540                       "(hostname = :HOSTNAME OR hostname IS NULL) "
00541                       "ORDER BY hostname DESC;" );
00542 
00543         query.bindValue(":KEY"     , sKey      );
00544         query.bindValue(":HOSTNAME", sHostName );
00545 
00546         if (!query.exec())
00547         {
00548             // clean up unused object we created.
00549 
00550             delete pList;
00551 
00552             MythDB::DBError("MythAPI::GetSetting() w/key ", query);
00553 
00554             throw( QString( "Database Error executing query." ));
00555         }
00556 
00557         if (query.next())
00558         {
00559             if ( (sHostName.isEmpty()) ||
00560                  ((!sHostName.isEmpty()) &&
00561                   (sHostName == query.value(1).toString())))
00562             {
00563                 pList->setHostName( query.value(1).toString() );
00564 
00565                 pList->Settings().insert( sKey, query.value(0) );
00566             }
00567         }
00568     }
00569     else
00570     {
00571         // ------------------------------------------------------------------
00572         // Looking to return all Setting for supplied hostname 
00573         // ------------------------------------------------------------------
00574 
00575         if (sHostName.isEmpty())
00576         {
00577             query.prepare("SELECT value, data FROM settings "
00578                           "WHERE (hostname IS NULL)" );
00579         }
00580         else
00581         {
00582             query.prepare("SELECT value, data FROM settings "
00583                           "WHERE (hostname = :HOSTNAME)" );
00584 
00585             query.bindValue(":HOSTNAME", sHostName );
00586         }
00587 
00588         if (!query.exec())
00589         {
00590             // clean up unused object we created.
00591 
00592             delete pList;
00593 
00594             MythDB::DBError("MythAPI::GetSetting() w/o key ", query);
00595             throw( QString( "Database Error executing query." ));
00596         }
00597 
00598         while (query.next())
00599             pList->Settings().insert( query.value(0).toString(), query.value(1) );
00600     }
00601 
00602     // ----------------------------------------------------------------------
00603     // Not found, so return the supplied default value
00604     // ----------------------------------------------------------------------
00605 
00606     if (pList->Settings().count() == 0)
00607         pList->Settings().insert( sKey, sDefault );
00608 
00609     return pList;
00610 }
00611 
00613 //
00615 
00616 bool Myth::PutSetting( const QString &sHostName, 
00617                        const QString &sKey, 
00618                        const QString &sValue ) 
00619 {
00620     bool bResult = false;
00621 
00622     if (!sKey.isEmpty())
00623     {
00624         if ( gCoreContext->SaveSettingOnHost( sKey, sValue, sHostName ) )
00625             bResult = true;
00626 
00627         return bResult;
00628     }
00629 
00630     throw ( QString( "Key Required" ));
00631 }
00632 
00634 //
00636 
00637 bool Myth::ChangePassword( const QString   &sUserName,
00638                            const QString   &sOldPassword,
00639                            const QString   &sNewPassword )
00640 {
00641     bool bResult = false;
00642 
00643     if (sUserName.isEmpty())
00644     {
00645         throw ( QString( "UserName not supplied when trying to change "
00646                          "password." ) );
00647     }
00648 
00649     if (sOldPassword.isEmpty())
00650     {
00651         throw ( QString( "Old Password not supplied when trying to change "
00652                          "password for '%1'." ).arg(sUserName) );
00653     }
00654 
00655     if (sNewPassword.isEmpty())
00656     {
00657         throw ( QString( "New Password not supplied when trying to change "
00658                          "password for '%1'." ).arg(sUserName) );
00659     }
00660 
00661     QCryptographicHash crypto( QCryptographicHash::Sha1 );
00662 
00663     crypto.addData( sOldPassword.toUtf8() );
00664 
00665     QString sPasswordHash( crypto.result().toBase64() );
00666 
00667     if ( sPasswordHash != gCoreContext->GetSetting( "HTTP/Protected/Password", ""))
00668     {
00669         throw ( QString( "Incorrect Old Password supplied when trying to "
00670                          "change password for '%1'." ).arg(sUserName) );
00671     }
00672 
00673     crypto.reset();
00674     crypto.addData( sNewPassword.toUtf8() );
00675 
00676     if (gCoreContext->SaveSettingOnHost( "HTTP/Protected/Password", crypto.result().toBase64(),
00677                                     QString() ) )
00678     {
00679         gCoreContext->ClearSettingsCache();
00680         bResult = true;
00681     }
00682 
00683     return bResult;
00684 }
00685 
00687 //
00689 
00690 bool Myth::TestDBSettings( const QString &sHostName,
00691                            const QString &sUserName,
00692                            const QString &sPassword,
00693                            const QString &sDBName,
00694                            int   dbPort)
00695 {
00696     bool bResult = false;
00697 
00698     QString db("mythconverg");
00699     int port = 3306;
00700 
00701     if (!sDBName.isEmpty())
00702         db = sDBName;
00703 
00704     if (dbPort != 0)
00705         port = dbPort;
00706 
00707     bResult = TestDatabase(sHostName, sUserName, sPassword, db, port);
00708 
00709     return bResult;
00710 }
00711 
00713 //
00715 
00716 bool Myth::SendMessage( const QString &sMessage,
00717                         const QString &sAddress,
00718                         int   udpPort,
00719                         int   Timeout)
00720 {
00721     bool bResult = false;
00722 
00723     if (sMessage.isEmpty())
00724         return bResult;
00725 
00726     if (Timeout < 0 || Timeout > 999)
00727         Timeout = 0;
00728 
00729     QString xmlMessage =
00730         "<mythmessage version=\"1\">\n"
00731         "  <text>" + sMessage + "</text>\n"
00732         "  <timeout>" + QString::number(Timeout) + "</timeout>\n"
00733         "</mythmessage>";
00734 
00735     QHostAddress address = QHostAddress::Broadcast;
00736     unsigned short port = 6948;
00737 
00738     if (!sAddress.isEmpty())
00739         address.setAddress(sAddress);
00740 
00741     if (udpPort != 0)
00742         port = udpPort;
00743 
00744     QUdpSocket *sock = new QUdpSocket();
00745     QByteArray utf8 = xmlMessage.toUtf8();
00746     int size = utf8.length();
00747 
00748     if (sock->writeDatagram(utf8.constData(), size, address, port) < 0)
00749     {
00750         LOG(VB_GENERAL, LOG_ERR,
00751             QString("Failed to send UDP/XML packet (Message: %1 "
00752                     "Address: %2 Port: %3")
00753                 .arg(sMessage).arg(sAddress).arg(port));
00754     }
00755     else
00756     {
00757         LOG(VB_GENERAL, LOG_DEBUG, 
00758             QString("UDP/XML packet sent! (Message: %1 Address: %2 Port: %3")
00759                 .arg(sMessage)
00760                 .arg(address.toString().toLocal8Bit().constData()).arg(port));
00761         bResult = true;
00762     }
00763 
00764     sock->deleteLater();
00765 
00766     return bResult;
00767 }
00768 
00770 //
00772 
00773 bool Myth::BackupDatabase(void)
00774 {
00775     bool bResult = false;
00776 
00777     DBUtil *dbutil = new DBUtil();
00778     MythDBBackupStatus status = kDB_Backup_Unknown;
00779     QString filename;
00780 
00781     LOG(VB_GENERAL, LOG_NOTICE, "Performing API invoked DB Backup.");
00782 
00783     if (dbutil)
00784         status = dbutil->BackupDB(filename);
00785 
00786     if (status == kDB_Backup_Completed)
00787     {
00788         LOG(VB_GENERAL, LOG_NOTICE, "Database backup succeeded.");
00789         bResult = true;
00790     }
00791     else
00792         LOG(VB_GENERAL, LOG_ERR, "Database backup failed.");
00793 
00794     delete dbutil;
00795 
00796     return bResult;
00797 }
00798 
00800 //
00802 
00803 bool Myth::CheckDatabase( bool repair )
00804 {
00805     bool bResult = false;
00806 
00807     DBUtil *dbutil = new DBUtil();
00808 
00809     LOG(VB_GENERAL, LOG_NOTICE, "Performing API invoked DB Check.");
00810 
00811     if (dbutil)
00812         bResult = dbutil->CheckTables(repair);
00813 
00814     if (bResult)
00815         LOG(VB_GENERAL, LOG_NOTICE, "Database check complete.");
00816     else
00817         LOG(VB_GENERAL, LOG_ERR, "Database check failed.");
00818 
00819     delete dbutil;
00820 
00821     return bResult;
00822 }
00823 
00825 //
00827 
00828 bool Myth::ProfileSubmit()
00829 {
00830     bool bResult = false;
00831 
00832     HardwareProfile *profile = new HardwareProfile();
00833     if (profile)
00834     {
00835         LOG(VB_GENERAL, LOG_NOTICE, "Profile Submission...");
00836         profile->GenerateUUIDs();
00837         bResult = profile->SubmitProfile();
00838         if (bResult)
00839             LOG(VB_GENERAL, LOG_NOTICE, "Profile Submitted.");
00840     }
00841     delete profile;
00842 
00843     return bResult;
00844 }
00845 
00847 //
00849 
00850 bool Myth::ProfileDelete()
00851 {
00852     bool bResult = false;
00853 
00854     HardwareProfile *profile = new HardwareProfile();
00855     if (profile)
00856     {
00857         LOG(VB_GENERAL, LOG_NOTICE, "Profile Deletion...");
00858         profile->GenerateUUIDs();
00859         bResult = profile->DeleteProfile();
00860         if (bResult)
00861             LOG(VB_GENERAL, LOG_NOTICE, "Profile Deleted.");
00862     }
00863     delete profile;
00864 
00865     return bResult;
00866 }
00867 
00869 //
00871 
00872 QString Myth::ProfileURL()
00873 {
00874     QString sProfileURL;
00875 
00876     HardwareProfile *profile = new HardwareProfile();
00877     if (profile)
00878     {
00879         profile->GenerateUUIDs();
00880         sProfileURL = profile->GetProfileURL();
00881         LOG(VB_GENERAL, LOG_NOTICE, QString("ProfileURL: %1").arg(sProfileURL));
00882     }
00883     delete profile;
00884 
00885     return sProfileURL;
00886 }
00887 
00889 //
00891 
00892 QString Myth::ProfileUpdated()
00893 {
00894     QString sProfileUpdate;
00895 
00896     HardwareProfile *profile = new HardwareProfile();
00897     if (profile)
00898     {
00899         profile->GenerateUUIDs();
00900         QDateTime tUpdated;
00901         tUpdated = profile->GetLastUpdate();
00902         sProfileUpdate = tUpdated.toString(
00903                              gCoreContext->GetSetting( "DateFormat", "MM.dd.yyyy"));
00904     }
00905     delete profile;
00906 
00907     return sProfileUpdate;
00908 }
00909 
00911 //
00913 
00914 QString Myth::ProfileText()
00915 {
00916     QString sProfileText;
00917 
00918     HardwareProfile *profile = new HardwareProfile();
00919     if (profile)
00920         sProfileText = profile->GetHardwareProfile();
00921     delete profile;
00922 
00923     return sProfileText;
00924 }
00925 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends