00001 #include "hardwareprofile.h"
00002
00003
00004 #include <QStringList>
00005 #include <QDir>
00006 #include <QTextStream>
00007
00008
00009 #include "mythcorecontext.h"
00010 #include "mythdirs.h"
00011 #include "mythlogging.h"
00012 #include "mythsystem.h"
00013 #include "exitcodes.h"
00014
00015 const QString SMOLT_SERVER_LOCATION =
00016 QString("http://smolt.mythtv.org/");
00017 const QString SMOLT_TOKEN =
00018 QString("smolt_token-smolt.mythtv.org");
00019
00020 HardwareProfile::HardwareProfile() :
00021 m_uuid(QString()), m_publicuuid(QString()),
00022 m_lastUpdate(QDateTime()), m_hardwareProfile(QString())
00023 {
00024 m_uuid = gCoreContext->GetSetting("HardwareProfileUUID");
00025 m_publicuuid = gCoreContext->GetSetting("HardwareProfilePublicUUID");
00026 QString lastupdate = gCoreContext->GetSetting("HardwareProfileLastUpdated");
00027 m_lastUpdate = QDateTime::fromString(lastupdate, Qt::ISODate);
00028 }
00029
00030 HardwareProfile::~HardwareProfile()
00031 {
00032 }
00033
00034 void HardwareProfile::GenerateUUIDs(void)
00035 {
00036 QString fileprefix = GetConfDir() + "/HardwareProfile";
00037 QDir dir(fileprefix);
00038 if (!dir.exists())
00039 dir.mkdir(fileprefix);
00040
00041
00042
00043 QString fileUUID = GetPrivateUUIDFromFile();
00044
00045 if (fileUUID.isEmpty() && m_uuid.isEmpty())
00046 {
00047 LOG(VB_GENERAL, LOG_INFO,
00048 "No UUID in DB or File, generating new UUID...");
00049
00050 QString cmd = GetShareDir() + "hardwareprofile/sendProfile.py";
00051 QStringList args;
00052 args << "-p";
00053 MythSystem system(cmd, args, kMSRunShell | kMSStdOut | kMSBuffered);
00054
00055 system.Run();
00056 system.Wait();
00057 m_hardwareProfile = system.ReadAll();
00058 m_uuid = GetPrivateUUIDFromFile();
00059 }
00060 else if (fileUUID.isEmpty())
00061 {
00062 LOG(VB_GENERAL, LOG_INFO,
00063 QString("Writing Database UUID to local file: %1")
00064 .arg(m_uuid));
00065 WritePrivateUUIDToFile(m_uuid);
00066 }
00067 else if (m_uuid.isEmpty())
00068 {
00069 LOG(VB_GENERAL, LOG_INFO,
00070 QString("Profile UUID found in local file: %1")
00071 .arg(fileUUID));
00072 m_uuid = fileUUID;
00073 }
00074
00075
00076
00077 m_publicuuid = GetPublicUUIDFromFile();
00078 }
00079
00080 QString HardwareProfile::GetPrivateUUIDFromFile() const
00081 {
00082 QString ret;
00083
00084 QString hwuuid_file = GetConfDir() + "/HardwareProfile/hw-uuid";
00085 QFile file(hwuuid_file);
00086 if (file.open( QIODevice::ReadOnly ))
00087 {
00088 QTextStream stream(&file);
00089 ret = stream.readLine();
00090 file.close();
00091 }
00092
00093 return ret;
00094 }
00095
00096 QString HardwareProfile::GetPublicUUIDFromFile() const
00097 {
00098 QString ret;
00099
00100 QString pubuuid_file = GetConfDir() + "/HardwareProfile/uuiddb.cfg";
00101 QString pubuuid;
00102 QFile pubfile(pubuuid_file);
00103 if (pubfile.open( QIODevice::ReadOnly ))
00104 {
00105 QString s;
00106 QTextStream stream(&pubfile);
00107 while ( !stream.atEnd() )
00108 {
00109 s = stream.readLine();
00110 if (s.contains(m_uuid))
00111 {
00112 ret = s.section("=",1,1);
00113 ret = ret.trimmed();
00114 }
00115 }
00116 pubfile.close();
00117 }
00118
00119 return ret;
00120 }
00121
00122 QString HardwareProfile::GetAdminPasswordFromFile() const
00123 {
00124 QString ret;
00125
00126 if (gCoreContext->GetSetting("HardwareProfileUUID").isEmpty())
00127 return ret;
00128
00129 QString token_file = GetConfDir() + "/HardwareProfile/" + SMOLT_TOKEN;
00130 QFile file(token_file);
00131 if (file.open( QIODevice::ReadOnly ))
00132 {
00133 QTextStream stream(&file);
00134 ret = stream.readLine();
00135 file.close();
00136 }
00137
00138 return ret;
00139 }
00140
00141 bool HardwareProfile::WritePrivateUUIDToFile(QString uuid)
00142 {
00143 QString hwuuid_file = GetConfDir() + "/HardwareProfile/hw-uuid";
00144 QFile file(hwuuid_file);
00145 if (file.open(QIODevice::WriteOnly))
00146 {
00147 QTextStream stream(&file);
00148 stream << uuid;
00149 file.close();
00150 return true;
00151 }
00152 else
00153 return false;
00154 }
00155
00156 bool HardwareProfile::NeedsUpdate(void) const
00157 {
00158 if (!m_lastUpdate.isNull() &&
00159 (m_lastUpdate.addMonths(1) < QDateTime::currentDateTime()) &&
00160 !m_uuid.isEmpty())
00161 {
00162 LOG(VB_GENERAL, LOG_INFO,
00163 "Last hardware profile update was > 30 days ago, update "
00164 "required...");
00165 return true;
00166 }
00167
00168 return false;
00169 }
00170
00171 bool HardwareProfile::SubmitProfile(void)
00172 {
00173 if (m_uuid.isEmpty())
00174 return false;
00175
00176 if (!m_hardwareProfile.isEmpty())
00177 LOG(VB_GENERAL, LOG_INFO,
00178 QString("Submitting the following hardware profile: %1")
00179 .arg(m_hardwareProfile));
00180
00181 QString cmd = GetShareDir() + "hardwareprofile/sendProfile.py";
00182 QStringList args;
00183 args << "--submitOnly";
00184 args << "-a";
00185 MythSystem system(cmd, args, kMSRunShell | kMSStdOut | kMSBuffered);
00186
00187 system.Run();
00188 if (system.Wait() == GENERIC_EXIT_OK)
00189 {
00190 GenerateUUIDs();
00191 gCoreContext->SaveSetting("HardwareProfileUUID", GetPrivateUUID());
00192 gCoreContext->SaveSetting("HardwareProfilePublicUUID", GetPublicUUID());
00193 gCoreContext->SaveSetting("HardwareProfileLastUpdated",
00194 QDateTime::currentDateTime().toString(Qt::ISODate));
00195 return true;
00196 }
00197 else
00198 return false;
00199
00200 return false;
00201 }
00202
00203 bool HardwareProfile::DeleteProfile(void)
00204 {
00205 if (m_uuid.isEmpty())
00206 return false;
00207
00208 LOG(VB_GENERAL, LOG_INFO,
00209 QString("Deleting the following hardware profile: %1")
00210 .arg(m_uuid));
00211
00212 QString cmd = GetShareDir() + "hardwareprofile/deleteProfile.py";
00213 QStringList args;
00214 MythSystem system(cmd, args, kMSRunShell | kMSStdOut | kMSBuffered);
00215
00216 system.Run();
00217 if (system.Wait() == GENERIC_EXIT_OK)
00218 {
00219 gCoreContext->SaveSetting("HardwareProfileUUID", "");
00220 gCoreContext->SaveSetting("HardwareProfilePublicUUID", "");
00221 gCoreContext->SaveSetting("HardwareProfileLastUpdated",
00222 QDateTime::currentDateTime().toString(Qt::ISODate));
00223 return true;
00224 }
00225 else
00226 return false;
00227
00228 return false;
00229 }
00230
00231 QString HardwareProfile::GetProfileURL() const
00232 {
00233 QString ret;
00234
00235 if (!gCoreContext->GetSetting("HardwareProfileUUID").isEmpty())
00236 {
00237 ret = SMOLT_SERVER_LOCATION + "client/show/?uuid=" + m_publicuuid;
00238 }
00239
00240 return ret;
00241 }
00242
00243 QString HardwareProfile::GetHardwareProfile() const
00244 {
00245 QString cmd = GetShareDir() + "hardwareprofile/sendProfile.py";
00246 QStringList args;
00247 args << "-p";
00248 MythSystem system(cmd, args, kMSRunShell | kMSStdOut | kMSBuffered);
00249
00250 system.Run();
00251 system.Wait();
00252 return system.ReadAll();
00253 }