[mythtv] patch: setsetting -> db

Andy Davidoff dert at pobox.com
Mon Feb 24 02:44:47 EST 2003


This patch makes MythContext::SetSetting() save settings into the
database.  It defaults to updating settings for localhost context,
but you can give it an optional 'true' argument to affect the global
value for the given key.
-------------- next part --------------
Index: mythcontext.cpp
===================================================================
RCS file: /var/lib/cvs/MC/libs/libmyth/mythcontext.cpp,v
retrieving revision 1.45
diff -d -u -w -r1.45 mythcontext.cpp
--- mythcontext.cpp	22 Feb 2003 13:01:20 -0000	1.45
+++ mythcontext.cpp	24 Feb 2003 07:38:46 -0000
@@ -497,7 +497,52 @@
 
 void MythContext::SetSetting(const QString &key, const QString &newValue)
 {
+    SetSetting(key, newValue, false);
+}
+
+void MythContext::SetSetting(const QString &key, const QString &newValue, bool global)
+{
+    QString where;
+    QString query;
+
     m_settings->SetSetting(key, newValue);
+
+    where = QString("WHERE value = '%1'").arg(key);
+
+    if (global)
+    {
+        where += " AND (hostname = '' OR hostname IS NULL)";
+    }
+    else
+    {
+        where += " AND hostname = '" + m_localhostname + "'";
+    }
+
+    pthread_mutex_lock(&dbLock);
+    if (m_db->isOpen()) 
+    {
+        KickDatabase(m_db);
+
+        QSqlQuery result = QSqlQuery("SELECT data FROM settings "
+                               + where + " LIMIT 1;", m_db);
+
+        if (0 < result.size()) 
+        {
+            if (newValue != result.value(0).asString())
+                query = QString("UPDATE settings SET data = '%1' %2;")
+                                .arg(newValue).arg(where);
+            QSqlQuery(query, m_db);
+        }
+        else
+        {
+            query = QString("INSERT INTO settings (value, data, hostname) "
+                            "VALUES ('%1', '%2', '%3');")
+                             .arg(key).arg(newValue)
+                             .arg(global ? "" : m_localhostname);
+            QSqlQuery(query, m_db);
+        }
+    }
+    pthread_mutex_unlock(&dbLock);
 }
 
 void MythContext::SendReceiveStringList(QStringList &strlist)
Index: mythcontext.h
===================================================================
RCS file: /var/lib/cvs/MC/libs/libmyth/mythcontext.h,v
retrieving revision 1.35
diff -d -u -w -r1.35 mythcontext.h
--- mythcontext.h	16 Feb 2003 19:25:37 -0000	1.35
+++ mythcontext.h	24 Feb 2003 07:38:46 -0000
@@ -78,6 +78,7 @@
     int GetNumSetting(const QString &key, int defaultval = 0);
 
     void SetSetting(const QString &key, const QString &newValue);
+    void SetSetting(const QString &key, const QString &newValue, bool global);
 
     int GetBigFontSize() { return GetNumSetting("QtFontBig", 25); }
     int GetMediumFontSize() { return GetNumSetting("QtFontMedium", 16); }


More information about the mythtv-dev mailing list